DATA Step, Macro, Functions and more

Macro: Calculations, creating a new variable

Reply
Regular Contributor
Posts: 194

Macro: Calculations, creating a new variable

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!

 




Super User
Posts: 5,503

Re: Macro: Calculations, creating a new variable

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.

Regular Contributor
Posts: 195

Re: Macro: Calculations, creating a new variable

     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

Respected Advisor
Posts: 3,799

Re: Macro: Calculations, creating a new variable

Posted in reply to UrvishShah

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

Regular Contributor
Posts: 195

Re: Macro: Calculations, creating a new variable

Posted in reply to data_null__

Regular Contributor
Posts: 194

Re: Macro: Calculations, creating a new variable

Posted in reply to data_null__

Thank you everyone!!

Super User
Posts: 10,023

Re: Macro: Calculations, creating a new variable

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

then call execute that macro .




Ksharp

Ask a Question
Discussion stats
  • 6 replies
  • 319 views
  • 5 likes
  • 5 in conversation