DATA Step, Macro, Functions and more

Proc Freq

Accepted Solution Solved
Reply
Contributor
Posts: 21
Accepted Solution

Proc Freq

Hi,

I'm a basic learner in sas,

 col1col2col3
ACE inhibitors3371
angiotensin receptor blockers100
Diuretic151
aldosterone antagonists/Diuretic100
alpha blockers200
angiotensin receptor blockers492311
angiotensin receptor blockers/ACE inhibitors100
angiotensin receptor blockers/Diuretic9161
antacids100
anthelmintic drug210
Antibiotics6183
anticonvulsant200

 

I'm looking for the count of all generic name in each column. how do i get this.

Please help me to solve.

 


Accepted Solutions
Solution
2 weeks ago
Super User
Super User
Posts: 9,599

Re: Proc Freq

Posted in reply to Sathish_jammy

Step 1 - Drop the transposed layout.

Step 2 - Freq

E.g.:

data have;
  input a $ b $ c $ d $ e $;
cards;
ca gv ca rr ds
ds ss ds ca ca
ds gv ca rr ca
gv ca ds rr ds
run;

data inter (keep=v r);
  set have;
  array tmp{5} a b c d e;
  do i=1 to 5;
    v=vname(tmp{i});
    r=tmp{i};
    output;
  end;
run;

proc freq data=inter;
  tables r*v;
run;

View solution in original post


All Replies
Super User
Super User
Posts: 9,599

Re: Proc Freq

Posted in reply to Sathish_jammy

Ther are a lot of SAS provided video tutorials, documents, and examples provided already which explain basic things like this:

https://video.sas.com/category/videos/how-to-tutorials

If you search there, there is a video which explains frequency counts.

And the docs have examples:

https://support.sas.com/documentation/cdl/en/statug/63962/HTML/default/viewer.htm#statug_freq_sect02...

 

If you want us to provide examples then you need to provide test data in the form of a datastep.

Contributor
Posts: 21

Re: Proc Freq

[ Edited ]
data aaa;
input a b c d e;
cards;
ca gv ca rr ds
ds ss ds ca ca
ds gv ca rr ca
gv ca ds rr ds
run;

Output

 Should be like

 

 abde
ca11212
gv12000
rr00030
ds20202
ss01000

 

Please share the code to resolve this kind of stuff.

Super User
Posts: 10,217

Re: Proc Freq

Posted in reply to Sathish_jammy

First of all, make it a point to always test code before posting it; a data step with example data has to run without any glitch.

Use a sequence of transposes before you run the proc freq, and transpose back after the freq:

data aaa;
input a $ b $ c $ d $ e $;
cards;
ca gv ca rr ds
ds ss ds ca ca
ds gv ca rr ca
gv ca ds rr ds
run;

proc transpose data=aaa out=int prefix=x_;
var _all_;
run;

proc transpose data=int (rename=(_name_=varname)) out=int1;
by varname;
var x_:;
run;

proc freq data=int1 noprint;
by varname;
tables col1 / out=int2;
run;

proc sort data=int2;
by col1;
run;

proc transpose data=int2 out=int3 (drop=_name_ _label_);
by col1;
id varname;
var count;
run;

data want;
set int3;
array nums {*} _numeric_;
do i = 1 to dim(nums);
  if missing(nums{i}) then nums{i} = 0;
end;
drop i;
run;

proc print data=want noobs;
run;

Result:

COL1    a    b    c    d    e

 ca     1    1    2    1    2
 ds     2    0    2    0    2
 gv     1    2    0    0    0
 rr     0    0    0    3    0
 ss     0    1    0    0    0
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
2 weeks ago
Super User
Super User
Posts: 9,599

Re: Proc Freq

Posted in reply to Sathish_jammy

Step 1 - Drop the transposed layout.

Step 2 - Freq

E.g.:

data have;
  input a $ b $ c $ d $ e $;
cards;
ca gv ca rr ds
ds ss ds ca ca
ds gv ca rr ca
gv ca ds rr ds
run;

data inter (keep=v r);
  set have;
  array tmp{5} a b c d e;
  do i=1 to 5;
    v=vname(tmp{i});
    r=tmp{i};
    output;
  end;
run;

proc freq data=inter;
  tables r*v;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 121 views
  • 1 like
  • 3 in conversation