Hi, I have a list of 32 variables (Q1, Q2,....Q31, Q32) and I would like to rename them (Q1_base, Q2_base,...,Q31_base, Q32_base). I am trying to figure out to code for a do loop that would do this fairly easily. All the variables are located in the same dataset. Any help is much appreciated! Thank you!
In a SAS data step, use
rename q1-q32=base_q1-base_q32;
If you need to have q1_base, then try this (assuming you have no other variable names that begin with Q): MODIFIED slightly as suggested by @FreelanceReinh
proc contents data=have noprint out=_contents_;
run;
proc sql;
select cats(name,'=',name,'_base') into :varnames separated by ' '
from _contents_ where upcase(name) eqt 'Q';
quit;
proc datasets library=work nolist;
modify have;
rename &varnames;
run;
quit;
This is unbelievably easy if you want the new variables to be named Base_Q1 through Base_Q32, no loops necessary. But, if they must be q1_base through q32_base, then I think you could use PROC SQL and macro variables to get this done.
So which is it? Can the variables be Base_Q1 through Base_Q32, or not?
Having them be Base_Q1 would be fine.
Would you mind showing me how to do both ways though, just out of curiosity?
Thank you!
In a SAS data step, use
rename q1-q32=base_q1-base_q32;
If you need to have q1_base, then try this (assuming you have no other variable names that begin with Q): MODIFIED slightly as suggested by @FreelanceReinh
proc contents data=have noprint out=_contents_;
run;
proc sql;
select cats(name,'=',name,'_base') into :varnames separated by ' '
from _contents_ where upcase(name) eqt 'Q';
quit;
proc datasets library=work nolist;
modify have;
rename &varnames;
run;
quit;
Thank you so much!
Just for completeness:
... into :varnames separated by ' ' from _contents_ ...
Otherwise, only the first variable would be renamed.
@FreelanceReinh wrote:
Just for completeness:
... into :varnames separated by ' ' from _contents_ ...Otherwise, only the first variable would be renamed.
Oh yeah, thanks ... that's always a problem when you can't test your code on the actual situation
This is an example using this attached %expand_varlist macro. It uses a similar technique to that shown by @PaigeMiller packed into a macro.
183 data q;
184 length q1-q35 $1;
185 stop;
186 call missing(of _all_);
187 run;
NOTE: The data set WORK.Q has 0 observations and 35 variables.
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
188
189 %let rename=%expand_varlist(data=q,var=q:,expr=cats(_name_,'=',_name_,'_BASE'));
NOTE: M=EXPAND_VARLIST_3 is a unique symbol name
190 %put NOTE: &=rename;
NOTE: RENAME=q1=q1_BASE q2=q2_BASE q3=q3_BASE q4=q4_BASE q5=q5_BASE q6=q6_BASE q7=q7_BASE q8=q8_BASE q9=q9_BASE q10=q10_BASE
q11=q11_BASE q12=q12_BASE q13=q13_BASE q14=q14_BASE q15=q15_BASE q16=q16_BASE q17=q17_BASE q18=q18_BASE q19=q19_BASE
q20=q20_BASE q21=q21_BASE q22=q22_BASE q23=q23_BASE q24=q24_BASE q25=q25_BASE q26=q26_BASE q27=q27_BASE q28=q28_BASE
q29=q29_BASE q30=q30_BASE q31=q31_BASE q32=q32_BASE q33=q33_BASE q34=q34_BASE q35=q35_BASE
I'm going to steal your macro and add it to my AUTOCALL library. Thanks, @data_null__!
@PaigeMiller I hope you find it useful. I think it is a nice application of DOSUBL. Using PROC TRANSPOSE to generate the variable list has a number of advantages including keeping the expanded variable list in the proper order.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.