简体   繁体   中英

SAS join on a flag to indicate percentile

I am looking to join two tables together

Table 1 - The baseball dataset

DATA baseball;
    SET sashelp.baseball
        (KEEP = crhits);
RUN;

Table 2 - A table containing the percentiles of CRhits

PROC STDIZE 
    DATA = baseball
    OUT=_NULL_      
    PCTLMTD=ORD_STAT
    PCTLDEF=5
    OUTSTAT=STDLONGPCTLS
        (WHERE = (SUBSTR(_TYPE_,1,1) = "P"))
    pctlpts = 1 TO 99 BY 1;
RUN;

I would like to join these tables together to create a table that contains the values for crhits and then a column identifying which percentile that value belongs to like below

crhits    percentile    percentile_value
54        p3            54
66        p5            66
825       p63           825
1134      p76           1133

The last column indicates the percentile value given by stdlongpctls

I currently use the following code to calculate the percentiles and a loop to count the number of "Events" per percentile, per factor

I have tried a cross-join but I am having trouble visualising how to join these two tables without an explicit key

PROC SQL;
    CREATE TABLE cross_join_table   AS
    SELECT
          a.crhits
        , b._TYPE_
        , CASE WHEN
            a.crhits < b.type       THEN b._TYPE_   END AS percentile
    FROM
        baseball                    a
    CROSS JOIN
        stdlongpctls                b;
QUIT;

If there is another easier / more efficient way to find the number of observations and number of dependent variables (eg I am modelling on a default flag event in my actual dataset, so the sum of 1's per percentile group, I would appreciate it)

Use PROC RANK instead to group it into the percentiles.

proc rank data=sashelp.baseball out=baseball_ranks group=100;
var crhits;
rank rank_crhits;
run;

You can then summarize it using PROC MEANS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM