BookmarkSubscribeRSS Feed
jcis7
Pyrite | Level 9

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!

 




6 REPLIES 6
Astounding
PROC Star

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.

UrvishShah
Fluorite | Level 6

     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

data_null__
Jade | Level 19

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...

UrvishShah
Fluorite | Level 6

jcis7
Pyrite | Level 9

Thank you everyone!!

Ksharp
Super User

Make a dataset contains "follow nofollow med vac1 vac2 vac3"

then call execute that macro .




Ksharp

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1378 views
  • 5 likes
  • 5 in conversation