SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Babloo
Rhodochrosite | Level 12

I would like to rename one variable only if it exists otherwise I should leave as such. In the below example, I'm getting error if variable 'id' doesn't exist. But I would like to conditionally execute the rename statement to get rid of the error.

 

data want;
set test; /*id variable sometimes exists with 'id' otherwise it will be like 'employee_id'*/ rename id=employee_id; /*rename statement should run only 'id' variable is available*/ run;
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@Babloo wrote:
Thanks. Let me slightly tweak the question. In your example, assume you are
calling one macro variable called &macrovariable. after SET statement
followed by rename statement as follows.

Data want;
set have;
&macrovariable.; /*resolves to some IF clause otherwise it will not resolve
to any value*/
rename id=employee_id;
run;

Now I want the above data step to be completed without any error if the
&macrovarible not resolves to any value.

Note there is a different option, DKROCOND, for drop/keep/rename options on OUTPUT datasets.

options dkrocond=nowarn;
data want(rename=(id=employee_id));
  set have;
  &macrovariable.;
run;

But it really dounds like you are trying to do this:

data want;
  set have;
%if %length(%superq(macrovariable)) %then %do;
  &macrovariable.;
  rename id=employee_id;
%end;
run;

 

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

Can you post your log please? I doubt that this gives your an error. Possibly a warning.

s_lassen
Meteorite | Level 14

You could use SQL, like this:

%let rename=; /* initialize, otherwise macro variable will not exist if no id variable */
proc sql noprint;
  select 'rename ID=Employee_id;' into :rename
  from dictionary.columns
  where libname='WORK' and memname='TEST' and name='ID';
quit;

data want;
  set test;
  &rename
run;
Babloo
Rhodochrosite | Level 12
Can we do in data step?
andreas_lds
Jade | Level 19

@Babloo wrote:
Can we do in data step?

Yes, of course, but the data-step will have to generate another data-step to do the renaming. This has been asked and answered so many times, please use the search-function to find code.

Tom
Super User Tom
Super User

So if ID doesn't exist you just want to copy the input dataset without any renaming?

You can probably just change the DKRICOND option setting.

Example:

862   options dkricond=warn;
863
864   data want;
865    set sashelp.class (rename=(id=employee_id));
WARNING: Variable id is not on file SASHELP.CLASS.
WARNING: The variable id in the DROP, KEEP, or RENAME list has never been referenced.
866    run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.03 seconds


867   options dkricond=nowarn;
868
869   data want;
870    set sashelp.class (rename=(id=employee_id));
871    run;

NOTE: There were 19 observations read from the data set SASHELP.CLASS.
NOTE: The data set WORK.WANT has 19 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


872   options dkricond=error;
873
874   data want;
875    set sashelp.class (rename=(id=employee_id));
ERROR: Variable id is not on file SASHELP.CLASS.
ERROR: Invalid DROP, KEEP, or RENAME option on file SASHELP.CLASS.
876    run;

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.WANT may be incomplete.  When this step was stopped there were 0 observations
         and 0 variables.
WARNING: Data set WORK.WANT was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
Babloo
Rhodochrosite | Level 12
Thanks. Let me slightly tweak the question. In your example, assume you are
calling one macro variable called &macrovariable. after SET statement
followed by rename statement as follows.

Data want;
set have;
&macrovariable.; /*resolves to some IF clause otherwise it will not resolve
to any value*/
rename id=employee_id;
run;

Now I want the above data step to be completed without any error if the
&macrovarible not resolves to any value.
Tom
Super User Tom
Super User

@Babloo wrote:
Thanks. Let me slightly tweak the question. In your example, assume you are
calling one macro variable called &macrovariable. after SET statement
followed by rename statement as follows.

Data want;
set have;
&macrovariable.; /*resolves to some IF clause otherwise it will not resolve
to any value*/
rename id=employee_id;
run;

Now I want the above data step to be completed without any error if the
&macrovarible not resolves to any value.

Note there is a different option, DKROCOND, for drop/keep/rename options on OUTPUT datasets.

options dkrocond=nowarn;
data want(rename=(id=employee_id));
  set have;
  &macrovariable.;
run;

But it really dounds like you are trying to do this:

data want;
  set have;
%if %length(%superq(macrovariable)) %then %do;
  &macrovariable.;
  rename id=employee_id;
%end;
run;

 

Babloo
Rhodochrosite | Level 12
Right!

Why we need %length macro?
Tom
Super User Tom
Super User

If the macro variable is empty then its length is zero.  You can use whatever test you want to check if the macro variable actually has a value or not.  Might be a better design to have whatever process created the macro variable help. Either by conditionally adding the RENAME statement to the value of the macro variable.  Or by setting some other macro variable that will be easier to test that tells whether or not the RENAME statement needs to be generated.

ballardw
Super User

@Babloo wrote:
Thanks. Let me slightly tweak the question. In your example, assume you are
calling one macro variable called &macrovariable. after SET statement
followed by rename statement as follows.

Data want;
set have;
&macrovariable.; /*resolves to some IF clause otherwise it will not resolve
to any value*/
rename id=employee_id;
run;

Now I want the above data step to be completed without any error if the
&macrovarible not resolves to any value.

RENAME is declarative, not executable. That means that it is not available conditionally. What ever is supposed to generate your "macro variable" would have to conditionally create either complete rename statement syntax or blank text.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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
  • 6123 views
  • 5 likes
  • 6 in conversation