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
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
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
IF I were you I would prefer SQL.
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 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!
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...
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;
Why not just include group into the tables statement?
proc freq data = dataset;
tables group*age;
run;
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
%macro freq(dsn,n);
proc freq data=&dsn;
where group=&n;
tables var;
run;
%mend;
%freq(datasetname,1);
%freq(datasetname,2);
%freq(datasetname,3);
Also what I was thinking of, great, thank you!
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
Yes, very helpful thank you:)
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.