Hi:
I am trying to automate a task. I create a macro variable as follows:
%Let weightvar = MATERIALS LABOR VOH FOH GNA INTEREST;
MATERIALS LABOR VOH FOH GNA INTEREST are field names in a database.
I weight average these in a PROC MEANS step.
Later, in a data step, I want to add these variables together to form a new field TOTALCOST.
However, I want to run the prog multiple times, each time adding or subtracting one or more of the fields in the weightvar line.
Obviously, TOTALCOST=sum(&weightvar); does not work. What are some options to effect this? Thanks
Not sure if I understand, but are you able to share the code? If the variables will remain constant, you can always you:
TotalCost = SUM(MATERIALS,LABOR,VOH,FOH,GNA,INTEREST) *Separated by commas;
If you want to execute them conditionally, such as adding or subtracting the variables, wrap it in a macro and use %IF statements to generate SAS code.
Use the TRANSLATE function to add commas.
You could either create a second macro variable at the top of your program or use it inline in your code as desired.
%macro addCommas(varList=);
%sysfunc(translate(&varList, %str(, ), %str( )))
%mend addCommas;
%let comma_delimited_list = %addCommas(varList = MATERIALS LABOR VOH FOH GNA INTEREST);
%put &comma_delimited_list;
You could use it directly inline with your code if you wanted, something like:
sumVariables = sum(%addCommas(varList = &weightVar.));
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.