Hello, I want to perform the same calcluation on 10+ variable (ie., need percentage).
How do I create a macro to do this? I copied and modified code from another program so my macro knowledge is minimal as you can tell.
Here's what I have:
%macro table (vars=);
%local (vars);
data new;
set old;
array vars {*} &vars;
do i=dim (vars);
??= (vars/total)*100; *want final variable for each to be: pct_follow, pct_nofollow, pct_med, etc);
end;
%mend;
%table (vars=follow nofollow med vac1 vac2 vac3);
Thank you!
You don't need a macro to do this. The reason to create a macro would be if you want to do this over and over, for different sets of variables each time.
If you do need a macro, the first step would be to sketch out what the program would look like for this one data set, without using any macro language. How could you accomplish what you want in this case, but without using macros? That's the starting point for understanding what a macro would look like. In particular, where does TOTAL come from? Is it already part of your data set?
Good luck.
data new;
set old;
array vars follow nofollow med vac1 vac2 vac3;
array vars_pct pct_follow pct_nofollow
pct_med pct_vac1 pct_vac2 pct_vac3;
do over vars;
pct_vars = (vars/total)*100;
end;
run;
If you have many variables needs to be include in ARRAY elements than let me know i will give you macro for that so that you dont need to type all the variables in array statements...
-Urvish
UrvishShah wrote:
If you have many variables needs to be include in ARRAY elements than let me know i will give you macro for that so that you dont need to type all the variables in array statements...
-Urvish
Is a MACRO really needed? I usually use a "SAS Variable List" http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#p0wphcpsfgx6o7n1sjtq...
data_null_; Yes Macro is not needed...We can directly refrence the range of variables as ARRAY elements...so simple...Why i opt for macro all the time ???
-Urvish
Thank you everyone!!
Make a dataset contains "follow nofollow med vac1 vac2 vac3"
then call execute that macro .
Ksharp
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.