Hello,
I'm trying to iterate over a list of variables in a data step, and for each one I want to create a new variable that is identical to the original but has the suffix "_new". Is this possible to do without resorting to using a macro (i.e. just in a data step)?
So, what I'm trying to achieve is something along the lines of (I know the below syntax is wrong):
%let varlist = varA varB varC ... varZ;
for each var in &varlist do;
var_new = var;
end;
Giving me a new set of varaibles varA_new etc. taat are identical to the originals. I've worked out a way to do this using %scan(&varlist, &i) in a macro, but this is a small bit of a much larger data step and it's not easy to create new macros in the project I'm working on.
SAS doesn't support that. It does support a few cases where the intent is to rename, rather than create new variables. So you will need a new approach, possibly this. Copy the entire data set to a new data set without change. Then make your calculations based on the new data set, using the original variable names. If necessary, find a way to combine the results of your calculations with the original data set.
By the way, how did you want to handle this if your original data set contains variable names that are 32 characters long?
SAS doesn't support that. It does support a few cases where the intent is to rename, rather than create new variables. So you will need a new approach, possibly this. Copy the entire data set to a new data set without change. Then make your calculations based on the new data set, using the original variable names. If necessary, find a way to combine the results of your calculations with the original data set.
By the way, how did you want to handle this if your original data set contains variable names that are 32 characters long?
You may want to consider making a common Prefix if you think you are going to want to do the same things or use them in a list.
If I variables abc cde and dfe and I create new varirable with a common suffix such as _pdq so that I have abc_pdq cde_pdq and dfe_pdq every time I want to address all of those variables I likely have to use the full variable name every time. However if I create variables
pdq_abc pdq_cde and pdq_dfe I can use a variable list such as pdq_: , the colon being one of the SAS list builders, and that will reference all varaibles that beging with pdq_ .
I don't understand the comment about: it's not easy to create new macros in the project
If you can add to the code you place a macro definition before it is needed then use as needed. Or place the macro in an autocall library and don't worry about including it in your code.
You could do this with the %include statement - not exactly macro, but not exactly regular sas statement either. This program produces these statments, the second of which is generated by the %INCLUDEd code:
data new;
set sashelp.class;
if 0 then set sashelp.class (rename=(Name=Name_new Sex=Sex_new
Age=Age_new Height=Height_new Weight=Weight_new));
run;
Here's how, using the VNEXT function:
filename tmp temp ;
data _null_;
set sashelp.class (obs=1);
length vnam $32 ;
file tmp;
put 'if 0 then set sashelp.class (rename=(';
do I=1 to 6;
call vnext(vnam);
if vnam='vnam' then leave;
put vnam +(-1) '=' vnam +(-1) '_new';
end;
put '));';
run;
data new;
set sashelp.class;
%include tmp /source2;
run;
%let varlist = varA varB varC ;
%let want=%sysfunc(prxchange(s/(\w+)/$1_new=$1/,-1,&varlist));
%put &want;
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.