DATA Step, Macro, Functions and more

Simple macro for repeating a proc freq for different levels of one variable

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

Simple macro for repeating a proc freq for different levels of one variable

Hello,

I have 3 mutually exclusively groups within my data set, each marked by a variable called "group" where group = 1,2, or 3.

I want to run a proc freq looking at age:

proc freq data = dataset;

     where group = 1;

     tables age;

run;

proc freq data = dataset;

     where group = 2;

     tables age;

run;

proc freq data = dataset;

     where group = 3;

     tables age;

run;

Is there a  more efficient way to do this using a macro? At first I had separated the groups into 3 mutually exclusive data sets and i was able to write a macro for this just changing the name of the data set. but now that it is in one data set, how can I accomplish this? Or would it be done using an array?thank you


Accepted Solutions
Solution
‎06-18-2013 03:00 AM
Regular Contributor
Posts: 195

Re: Simple macro for repeating a proc freq for different levels of one variable

Use the Iterative %DO Loops within the Macro Definition...

%macro freq (n);

   %do i = 1 %to &n.;

         

       proc freq data = dataset_name (where = (group = &i.));

           tables age;

       run;

   %end;

%mend;

%freq(10); /*Change the Value as per your requirement*/

-Urvish

View solution in original post


All Replies
Solution
‎06-18-2013 03:00 AM
Regular Contributor
Posts: 195

Re: Simple macro for repeating a proc freq for different levels of one variable

Use the Iterative %DO Loops within the Macro Definition...

%macro freq (n);

   %do i = 1 %to &n.;

         

       proc freq data = dataset_name (where = (group = &i.));

           tables age;

       run;

   %end;

%mend;

%freq(10); /*Change the Value as per your requirement*/

-Urvish

Super User
Posts: 10,018

Re: Simple macro for repeating a proc freq for different levels of one variable

IF I were you I would prefer SQL.

Contributor
Posts: 67

Re: Simple macro for repeating a proc freq for different levels of one variable

I think SQL is also useful, unfortunately I am not *too* familiar yet :T I took a one-day workshop on SQL and still have my notes, perhaps I should refer back to them. Thank you Smiley Happy You always answer my questions and are very helpful. I have also seen some of the handouts you have made online.. thank you very much for all your help!

Regular Contributor
Posts: 195

Re: Simple macro for repeating a proc freq for different levels of one variable

You can follow the SAS/SQL dictionary or user's manual...

http://support.sas.com/documentation/cdl/en/sqlproc/62086/PDF/default/sqlproc.pdf

Enjoy...

New Contributor
Posts: 4

Re: Simple macro for repeating a proc freq for different levels of one variable

Use the by variable. The only issue is that the data must be in order by group.

data in;

infile cards;

input age fld2 group; cards;

1 1 1

2 2 2

3 3 3

1 1 2

1 1 3

2 2 1

2 2 3

;

proc sort data=in;by group;

proc freq data = in;by group;    

tables age;

run;

Super User
Super User
Posts: 7,035

Re: Simple macro for repeating a proc freq for different levels of one variable

Why not just include group into the tables statement?

proc freq data = dataset;

      tables group*age;

run;

Contributor
Posts: 67

Re: Simple macro for repeating a proc freq for different levels of one variable

Wow, this is awesome... and so simple. I cannot believe I didn't think of this haha. Thank you very much! This is very helpful Smiley Happy

New Contributor
Posts: 2

Re: Simple macro for repeating a proc freq for different levels of one variable

%macro freq(dsn,n);

       proc freq data=&dsn;

       where group=&n;

       tables var;

       run;

%mend;

%freq(datasetname,1);

%freq(datasetname,2);

%freq(datasetname,3);

Contributor
Posts: 67

Re: Simple macro for repeating a proc freq for different levels of one variable

Posted in reply to gowthamgsas

Also what I was thinking of, great, thank you!

Super Contributor
Posts: 334

Re: Simple macro for repeating a proc freq for different levels of one variable

I think , all have valid solutions with the by group processing (terryfearn) closest to what you were trying to do with the macro. Tom's cross tab puts everything in a single printout, and KSharp SQL would be good if the dataset was extremely large and you didnt want to sort the source data but you have to code the stats you want directly (unless all you want is the counts of the different levels).

Hope the explanation helps!

EJ

Contributor
Posts: 67

Re: Simple macro for repeating a proc freq for different levels of one variable

Yes, very helpful thank youSmiley Happy

Contributor
Posts: 67

Re: Simple macro for repeating a proc freq for different levels of one variable

Thank you! I have marked this as the correct answer, although the others work too, because this is what I was thinking of/had in mind. Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 12 replies
  • 1027 views
  • 15 likes
  • 7 in conversation