BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Sathish_jammy
Lapis Lazuli | Level 10

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Sathish_jammy
Lapis Lazuli | Level 10
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.

Kurt_Bremser
Super User

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
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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