SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
aw016
Obsidian | Level 7

Hello,

I am trying to rename variables altogether based on the "session" value.

If session=1 then all variables should be var_bl; if session=2 then all variables should be var_12m.

I was wondering how to use concatenate function to do that. Previously I read some similar posts, but couldn't quite understand the algorithm.

Very much appreciate any explanation!!

5 REPLIES 5
Kurt_Bremser
Super User

Your question makes no sense. All variables in a dataset must have different names, so you cannot rename them to the same name.

 

If you have trouble describing your task, post examples for "before" and "after".

aw016
Obsidian | Level 7

Hi, what I meant is not to rename everything into the same name, but to add a suffix to all variables as "_bl" or "_12m" based on session value.

Kurt_Bremser
Super User

You retrieve the existing names from DICTIONARY.COLUMNS in PROC SQL.

With SELECT INTO you can build a macro variable which holds the rename pairs, for later use in PROC DATASETS.

%if &session = 1
%then %do;
%let suff = _bl;
%end;
%else %do;
%let suff = _12m;
%end;

proc sql noprint;
select catx("=",name,cats(name,"&suff.")) into :renames separated by " "
from dictionary.columns
where libname = "LIBRARY" and memname = "DATASET";
quit;

proc datasets lib=library;
modify dataset;
rename &renames.;
quit;
aw016
Obsidian | Level 7

Hi, thank you for your codes!

However, variables were just renamed to "_12m" instead of selected based on the session. I was wondering what would be the issue.

here is the code:

 

data one;
   input id  age session score1 score2 score3;
   datalines;
1 10 1 85 90 89
1  10 2 95 100 99
2  11 1 99 98 91
2   11 2 99 98 91
3  12 1 100 100 100
3  12 2 60 70 80
4   11 1 78 89 100
4 11 2 88 99 120
;
run;

%if &session = 1
%then %do;
%let suff = _bl;
%end;
%else %if &session=2 %do;
%let suff = _12m;
%end;

proc sql noprint;
select catx("=",name,cats(name,"&suff.")) into :renames separated by " "
from dictionary.columns
where libname = "WORK" and memname = "ONE";
quit;

proc datasets lib=work;
modify one;
rename &renames.;
quit;

Here is the log:

 

763 %if &session = 1
WARNING: Apparent symbolic reference SESSION not resolved.
764 %then %do;
WARNING: Apparent symbolic reference SESSION not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric
operand is required. The condition was: &session = 1
ERROR: Skipping to next %END statement.
765 %let suff = _bl;
766 %end;
767 %else %if &session=2 %do;
ERROR: Nesting of %IF statements in open code is not supported. %IF ignored.
ERROR: Skipping to next %END statement.
768 %let suff = _12m;
769 %end;
770

Kurt_Bremser
Super User

You cannot rename conditionally (on the value of a dataset variable). Your question therefore makes even less sense than before.

Please show the intended result for your example data.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 5 replies
  • 837 views
  • 0 likes
  • 2 in conversation