Hi,
I'm a basic learner in sas,
col1 | col2 | col3 | |
ACE inhibitors | 33 | 7 | 1 |
angiotensin receptor blockers | 1 | 0 | 0 |
Diuretic | 1 | 5 | 1 |
aldosterone antagonists/Diuretic | 1 | 0 | 0 |
alpha blockers | 2 | 0 | 0 |
angiotensin receptor blockers | 49 | 23 | 11 |
angiotensin receptor blockers/ACE inhibitors | 1 | 0 | 0 |
angiotensin receptor blockers/Diuretic | 9 | 16 | 1 |
antacids | 1 | 0 | 0 |
anthelmintic drug | 2 | 1 | 0 |
Antibiotics | 6 | 18 | 3 |
anticonvulsant | 2 | 0 | 0 |
I'm looking for the count of all generic name in each column. how do i get this.
Please help me to solve.
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;
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:
If you want us to provide examples then you need to provide test data in the form of a datastep.
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
a | b | c | d | e | |
ca | 1 | 1 | 2 | 1 | 2 |
gv | 1 | 2 | 0 | 0 | 0 |
rr | 0 | 0 | 0 | 3 | 0 |
ds | 2 | 0 | 2 | 0 | 2 |
ss | 0 | 1 | 0 | 0 | 0 |
Please share the code to resolve this kind of stuff.
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
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.