简体   繁体   English

SAS代码,用于多次输入主键和相应数据

[英]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 . 对于每个primary_id我必须创建一个对应的E_id的表。

How do I do it in SAS; 我该如何在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?? 如何概括所有主键的where语句?

The by statement is how you get a separate table for each ID. by语句是如何为每个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. 这是一个允许您选择p_id生成频率表的解决方案。

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; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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