Hello!
I have a dataset that contains the variable record_id. I am using the substr() function to filter by the first two numbers in this record_id variable and using a proc freq to create some tables. Example shown below:
proc freq data = import;
tables sex ethnicity age;
where substr(record_id,1,2)='14';
run;
What I would like to do is create a macro that changes the value of the substring to numbers between 13 - 19, as that encompasses all the prefixes for the record ID's. I'm completely inexperienced with macros so I'm not sure how to go about doing this, and I'm not sure whether to create a macro that encompasses the whole proc freq statement, or just the value of the substring. Any tips or help is appreciated.
Thanks!
Best,
Adam
How about this:
%macro dothis(string=);
proc freq data = import;
tables sex ethnicity age;
where substr(record_id,1,2)="&string";
run;
%mend;
How about this:
%macro dothis(string=);
proc freq data = import;
tables sex ethnicity age;
where substr(record_id,1,2)="&string";
run;
%mend;
So, now I have to comment, I think you should do this without macros.
Example:
data intermediate;
set import;
prefix=substr(record_id,1,2);
run;
proc freq data=intermediate;
tables prefix*(sex ethnicity age);
run;
Even shorter which takes advantage only wanting the first two characters. The format applied will have the output show just the first two characters. No need for additional variables or data set.
proc freq data=import; tables record_id*(sex ethnicity age); format record_id $2. ; run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.