DOT
DOT

Reputation: 329

List frequency of presence of each variable using loop in SAS

I tried some solutions already here and I am still unable to get a desired output.

The data I have is given below (ID is unique):

  data have;
    input id code_1 code_2 code_3 code_4 randa randb randc$;
    datalines;
    19736 1 0   1   0   5.5 10 11
    19737 0 0   0   1   2   4.8 19
    19738 1 0   1   1   6   9   2.6
    19739 1 1   0   1   1.6 7   8.5
    ;;;;;
    run

I need to get the frequency of only the presence of various codes. (code1, code2 etc..)

The desired output:

  Variable Frequency  

code_1      3

code_2      1 

code_3      2 

code_4      3 

I tried the solution in this and the code is given below:

ods output onewayfreqs=preds;
proc freq data=have;
tables _all_;
run;
ods output close;

proc tabulate data=preds;
class table frequency;
tables table,frequency;
run;

Output:

     Frequenza 
    1 2 3 
    N N N 
    Table 1 . 1 
    Tabella code_1  
    Tabella code_2  1 . 1 
    Tabella code_3  . 2 . 
    Tabella code_4  1 . 1 
    Tabella id  4 . . 
    Tabella randa  4 . . 
    Tabella randb  4 . . 
    Tabella randc  4 . . 

Also I tried as the code below:

proc freq data=have order=freq;
array codes code_:;
  do _n_ = 1 to dim(codes);
    table codes(_n_)/list missing out=var1_freq;
  end;
run;

But I donot know how to write the code properly.

I am getting output for the code below (only for one code at a time):

   proc freq data=have order=freq ;
    tables code_1/list missing out=var1_freq;
    run;

But how to get for multiple codes? Many thanks for your help..!

Upvotes: 0

Views: 380

Answers (1)

Richard
Richard

Reputation: 27508

The out= option for the tables statement will only produce output for the last variable listed, so you won't get all 4 codes.

You can count the 1 valued code_* variables after transposition.

data have;
input id code_1 code_2 code_3 code_4 randa randb randc $ ;
datalines;
19736 1 0 1 0  5.5 10    11
19737 0 0 0 1  2    4.8  19
19738 1 0 1 1  6    9     2.6
19739 1 1 0 1  1.6  7     8.5
;

data idcodes / view=idcodes;
  set have;
  array codes code_1-code_4;
  do _n_ = 1 to dim (codes);
    variable = vname(codes(_n_));
    flag = codes(_n_);
    output;
  end;
  keep id variable flag;
run;

proc freq data=idcodes;
  where flag;
  table variable / out=freqs(keep=variable count);
run;

Presuming codes are only 0/1, you could also sum the codes and transpose the result.

proc means noprint data=have;
  var code_:;
  output out=flagsum sum=;
run;

proc transpose data=flagsum out=want(rename=(_name_=variable col1=frequency));
  var code_:;
run;

Upvotes: 2

Related Questions