- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Babloo wrote:
Thanks. Let me slightly tweak the question. In your example, assume you are
calling one macro variable called ¯ovariable. after SET statement
followed by rename statement as follows.
Data want;
set have;
¯ovariable.; /*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
¯ovarible 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;
¯ovariable.;
run;
But it really dounds like you are trying to do this:
data want;
set have;
%if %length(%superq(macrovariable)) %then %do;
¯ovariable.;
rename id=employee_id;
%end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Can you post your log please? I doubt that this gives your an error. Possibly a warning.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
calling one macro variable called ¯ovariable. after SET statement
followed by rename statement as follows.
Data want;
set have;
¯ovariable.; /*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
¯ovarible not resolves to any value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Babloo wrote:
Thanks. Let me slightly tweak the question. In your example, assume you are
calling one macro variable called ¯ovariable. after SET statement
followed by rename statement as follows.
Data want;
set have;
¯ovariable.; /*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
¯ovarible 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;
¯ovariable.;
run;
But it really dounds like you are trying to do this:
data want;
set have;
%if %length(%superq(macrovariable)) %then %do;
¯ovariable.;
rename id=employee_id;
%end;
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Why we need %length macro?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@Babloo wrote:
Thanks. Let me slightly tweak the question. In your example, assume you are
calling one macro variable called ¯ovariable. after SET statement
followed by rename statement as follows.
Data want;
set have;
¯ovariable.; /*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
¯ovarible 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.