BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ginak
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
UrvishShah
Fluorite | Level 6

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

12 REPLIES 12
UrvishShah
Fluorite | Level 6

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

ginak
Quartz | Level 8

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!

terryfearn
Calcite | Level 5

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;

Tom
Super User Tom
Super User

Why not just include group into the tables statement?

proc freq data = dataset;

      tables group*age;

run;

ginak
Quartz | Level 8

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

gowthamgsas
Calcite | Level 5

%macro freq(dsn,n);

       proc freq data=&dsn;

       where group=&n;

       tables var;

       run;

%mend;

%freq(datasetname,1);

%freq(datasetname,2);

%freq(datasetname,3);

ginak
Quartz | Level 8

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

esjackso
Quartz | Level 8

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

ginak
Quartz | Level 8

Yes, very helpful thank you:)

ginak
Quartz | Level 8

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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