简体   繁体   中英

SAS code for multiple entries of primary key and corresponding data

I have data like below

p_id  E_id
----  ----
1     1  
1     2
1     3
1     4
2     1
3     1
3     2
3     3
4     1

For each primary_id I have to create a table of the corresponding E_id .

How do I do it in SAS;

I am using:

  proc freq data = abc;
     where p_id = 1;
     tables p_id * E_id;
  run;

How do I generalize the where statement for all the primary keys??

The by statement is how you get a separate table for each ID. It requires data to be sorted by the variable.

proc freq data = abc;
  by p_id;
  tables p_id * E_id; 
run;

Here is a solution allowing you to select p_id to generate frequency tables.

data have;
  input p_id e_id;
  datalines;
  1      1  
  1      2
  1      3
  1      4
  2      1
  3      1
  3      2
  3      3
  4      1
  ;
run;

proc sort data = have;
  by p_id;
run;

%let pid_list = (1,2); ** only generate two tables;
data _null_;
  set have;
  by p_id;
  if first.p_id and p_id in &pid_list then do;
    call execute('
                proc freq data = have(where = (p_id = '||p_id||'));
                    tables p_id * e_id;
                run;
                ');
  end;
run; 

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