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-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
  • 6 replies
  • 1029 views
  • 5 likes
  • 5 in conversation