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.));
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.