BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kmardinian
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;  

 

--
Paige Miller

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

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?

 

 

--
Paige Miller
kmardinian
Quartz | Level 8

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!

PaigeMiller
Diamond | Level 26

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;  

 

--
Paige Miller
kmardinian
Quartz | Level 8

Thank you so much!

FreelanceReinh
Jade | Level 19

Just for completeness:

... into :varnames separated by ' ' from _contents_ ...

Otherwise, only the first variable would be renamed. 

PaigeMiller
Diamond | Level 26

@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

--
Paige Miller
Reeza
Super User
If you use BASE_XXX you can also use the colon notation in future references which you cannot do if you use your requested notation. In general, in SAS, prefixes are better than suffixes.
data_null__
Jade | Level 19

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
PaigeMiller
Diamond | Level 26

I'm going to steal your macro and add it to my AUTOCALL library. Thanks, @data_null__!

--
Paige Miller
data_null__
Jade | Level 19

@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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 4723 views
  • 4 likes
  • 5 in conversation