<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Rename variable only if it exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607291#M176495</link>
    <description>&lt;P&gt;Can you post your log please? I doubt that this gives your an error. Possibly a warning.&lt;/P&gt;</description>
    <pubDate>Tue, 26 Nov 2019 12:04:05 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-11-26T12:04:05Z</dc:date>
    <item>
      <title>Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607289#M176493</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set test; &lt;CODE class=" language-sas" style="color: #333333; font-family: &amp;amp;quot; helevticaneue-light&amp;amp;quot;,&amp;amp;quot;helvetica neue&amp;amp;quot;,helvetica,arial,sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: left; text-decoration: none; text-indent: 0px; text-transform: none; -webkit-text-stroke-width: 0px; white-space: normal; word-spacing: 0px;"&gt;/*id variable sometimes exists with 'id' otherwise it will be like 'employee_id'*/&lt;/CODE&gt; rename id=employee_id; /*rename statement should run only 'id' variable is available*/ run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Nov 2019 11:57:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607289#M176493</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-11-26T11:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607291#M176495</link>
      <description>&lt;P&gt;Can you post your log please? I doubt that this gives your an error. Possibly a warning.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 12:04:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607291#M176495</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-26T12:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607296#M176497</link>
      <description>&lt;P&gt;You could use SQL, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%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;
  &amp;amp;rename
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Nov 2019 12:08:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607296#M176497</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-11-26T12:08:48Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607320#M176505</link>
      <description>Can we do in data step?&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Nov 2019 13:02:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607320#M176505</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-11-26T13:02:18Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607328#M176513</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Can we do in data step?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 13:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607328#M176513</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-11-26T13:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607353#M176531</link>
      <description>&lt;P&gt;So if ID doesn't exist you just want to copy the input dataset without any renaming?&lt;/P&gt;
&lt;P&gt;You can probably just change the DKRICOND option setting.&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;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
&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Nov 2019 14:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607353#M176531</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-26T14:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607365#M176542</link>
      <description>Thanks. Let me slightly tweak the question. In your example, assume you are&lt;BR /&gt;calling one macro variable called &amp;amp;macrovariable. after SET statement&lt;BR /&gt;followed by rename statement as follows.&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;amp;macrovariable.; /*resolves to some IF clause otherwise it will not resolve&lt;BR /&gt;to any value*/&lt;BR /&gt;rename id=employee_id;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Now I want the above data step to be completed without any error if the&lt;BR /&gt;&amp;amp;macrovarible not resolves to any value.&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Nov 2019 15:16:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607365#M176542</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-11-26T15:16:18Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607369#M176546</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks. Let me slightly tweak the question. In your example, assume you are&lt;BR /&gt;calling one macro variable called &amp;amp;macrovariable. after SET statement&lt;BR /&gt;followed by rename statement as follows.&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;amp;macrovariable.; /*resolves to some IF clause otherwise it will not resolve&lt;BR /&gt;to any value*/&lt;BR /&gt;rename id=employee_id;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Now I want the above data step to be completed without any error if the&lt;BR /&gt;&amp;amp;macrovarible not resolves to any value.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note there is a different option, DKROCOND, for drop/keep/rename options on OUTPUT datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options dkrocond=nowarn;
data want(rename=(id=employee_id));
  set have;
  &amp;amp;macrovariable.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But it really dounds like you are trying to do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
%if %length(%superq(macrovariable)) %then %do;
  &amp;amp;macrovariable.;
  rename id=employee_id;
%end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 15:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607369#M176546</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-26T15:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607384#M176556</link>
      <description>Right!&lt;BR /&gt;&lt;BR /&gt;Why we need %length macro?&lt;BR /&gt;</description>
      <pubDate>Tue, 26 Nov 2019 15:48:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607384#M176556</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-11-26T15:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607386#M176558</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks. Let me slightly tweak the question. In your example, assume you are&lt;BR /&gt;calling one macro variable called &amp;amp;macrovariable. after SET statement&lt;BR /&gt;followed by rename statement as follows.&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;set have;&lt;BR /&gt;&amp;amp;macrovariable.; /*resolves to some IF clause otherwise it will not resolve&lt;BR /&gt;to any value*/&lt;BR /&gt;rename id=employee_id;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Now I want the above data step to be completed without any error if the&lt;BR /&gt;&amp;amp;macrovarible not resolves to any value.&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 15:49:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607386#M176558</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-11-26T15:49:09Z</dc:date>
    </item>
    <item>
      <title>Re: Rename variable only if it exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607404#M176575</link>
      <description>&lt;P&gt;If the macro variable is empty then its length is zero.&amp;nbsp; You can use whatever test you want to check if the macro variable actually has a value or not.&amp;nbsp; 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.&amp;nbsp; 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.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Nov 2019 16:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rename-variable-only-if-it-exists/m-p/607404#M176575</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-26T16:09:30Z</dc:date>
    </item>
  </channel>
</rss>

