This is somewhat of an odd request, but hopefully someone can help me out.
I need to create new variables for each value entered into a %let statement. Ideally, I would like to have a statement:
%let summary_vars = ____________ where they would then list the summary vars (whether separated by spaces or commas, whichever works). I would then like those to each be a separate variable (can just be numbered), and to work on any number of vars entered.
So, for example:
%let summary_vars = outstanding, exercise, total
WOULD RESULT IN:
summary_vars1 = outstanding
summary_vars2 = exercise
summary_vars3 = total
Does anyone have any ideas?
Thanks!
Still not clear what the input is. Is it a macro variable? (that is what a %LET statement can create)
Still not clear what the output is. Do you want a SAS dataset as in the example from Art? Or just a report?
Assuming that you have an input macro variable that I will call VARLIST here is a simple data step to populate it.
%* Create an example input ;
%let varlist=total,outstanding,exercise,freq;
* Create dataset WANT using ARRAY statement with initial values ;
data want ;
array summary_var (%sysfunc(countw("&varlist",%str(,)))) $32
(%sysfunc(tranwrd("&varlist",%str(,),",")))
;
run;
proc print ;
run;
summary_ summary_ summary_ summary_
Obs var1 var2 var3 var4
1 total outstanding exercise freq
Hi:
It's not really clear to me what you are trying to do. How would you use summary_vars1, summary_vars2 and summary_vars3. Would "outstanding", "exercise" and "total" already be variables in a data sets? What code have you tried and what are you trying to achieve? What does your input data look like? And what are your desired results?
cynthia
Are you looking for something like the following?:
%let summary_vars = outstanding, exercise, total;
data test;
input outstanding exercise total;
cards;
1 2 3
4 5 6
;
data want;
set test;
array summary_vars(3);
_n_=1;
do while (scan("&summary_vars",_n_) ne "");
summary_vars(_n_)=vvaluex(scan("&summary_vars",_n_));
_n_+1;
end;
run;
Thank you both for your responses.
I apologize for my lack of clarity. There will be empty let statements for users to fill in. The summary_vars are not already variables, they are put in by user. The output should result in one line of results - the headers (summary_var1, summary_var2...) and the results (outstanding, total...). So it should look something like this:
Arthur - the data test portion is not needed because there is no additional information, but the data want looks promising. I need the array to be dynamic based on the number of items in summary_var %let statment. Then I would just need the values to be read into each summary_var in the array.
Thanks again!
Still not clear what the input is. Is it a macro variable? (that is what a %LET statement can create)
Still not clear what the output is. Do you want a SAS dataset as in the example from Art? Or just a report?
Assuming that you have an input macro variable that I will call VARLIST here is a simple data step to populate it.
%* Create an example input ;
%let varlist=total,outstanding,exercise,freq;
* Create dataset WANT using ARRAY statement with initial values ;
data want ;
array summary_var (%sysfunc(countw("&varlist",%str(,)))) $32
(%sysfunc(tranwrd("&varlist",%str(,),",")))
;
run;
proc print ;
run;
summary_ summary_ summary_ summary_
Obs var1 var2 var3 var4
1 total outstanding exercise freq
EXACTLY what I wanted. Thank you so much!
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.