<?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: Create new variable if don't exists in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589325#M168544</link>
    <description>I have to wrap your  code with macro as you started the code with IF?&lt;BR /&gt;</description>
    <pubDate>Tue, 17 Sep 2019 12:09:44 GMT</pubDate>
    <dc:creator>Babloo</dc:creator>
    <dc:date>2019-09-17T12:09:44Z</dc:date>
    <item>
      <title>Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589313#M168537</link>
      <description>&lt;P&gt;I'm executing the following code in most of the programs.. For the programs which don't have ENTITY_ID from the querying table, I want to create the variable ENTITY_ID (character)&amp;nbsp;and assign the missing value instead of receiving the error '&lt;EM&gt;ERROR: The following columns were not found in the contributing tables: ENTITY_ID.&amp;nbsp;'&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*extract unique entity_id*/
proc sql;
create table dis_entity_id as select distinct ENTITY_ID format=$ENTITY. from &amp;amp;library..&amp;amp;Input_table.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 11:37:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589313#M168537</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-17T11:37:06Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589317#M168538</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*extract unique entity_id*/
proc sql;
create table dis_entity_id as 
select *,' ' as  ENTITY_ID 
from &amp;amp;library..&amp;amp;Input_table.;
quit&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 11:51:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589317#M168538</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-17T11:51:09Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589321#M168541</link>
      <description>Well, if the data is available for ENTITY_ID from the querying table  then&lt;BR /&gt;I have to extract that value and not the missing value.&lt;BR /&gt;&lt;BR /&gt;So If the variable exists then extract that value else assign missing value&lt;BR /&gt;by creating that variable.&lt;BR /&gt;</description>
      <pubDate>Tue, 17 Sep 2019 11:59:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589321#M168541</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-17T11:59:44Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589322#M168542</link>
      <description>&lt;P&gt;Okay&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp; &amp;nbsp;Use this&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-variable-exists-in-a-dataset/td-p/475813" target="_blank"&gt;https://communities.sas.com/t5/SAS-Tips-from-the-Community/SAS-Tip-Check-if-a-variable-exists-in-a-dataset/td-p/475813&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the respective IF THEN ELSE, plug in your SQL code&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 12:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589322#M168542</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-17T12:02:41Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589323#M168543</link>
      <description>&lt;P&gt;So, this is how you plug your sql in that macro&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
 %if %sysfunc(varnum(&amp;amp;dsid, &amp;amp;var)) &amp;gt; 0 %then %do;
       /*extract unique entity_id*/
proc sql;
create table dis_entity_id as 
select distinct  ENTITY_ID 
from &amp;amp;library..&amp;amp;Input_table.;
quit;
    %end;
    %else %do;
       proc sql;
create table dis_entity_id as 
select *,' '  ENTITY_ID 
from &amp;amp;library..&amp;amp;Input_table.;
quit;
    %end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 12:05:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589323#M168543</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-17T12:05:35Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589325#M168544</link>
      <description>I have to wrap your  code with macro as you started the code with IF?&lt;BR /&gt;</description>
      <pubDate>Tue, 17 Sep 2019 12:09:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589325#M168544</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-17T12:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589327#M168545</link>
      <description>&lt;P&gt;Yes, please read the comments, you will understand.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Basically, first, these are steps&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Check if the variable already exists&lt;/P&gt;
&lt;P&gt;2. If exists, run a plain extract query&lt;/P&gt;
&lt;P&gt;3. If not, assign a new variable with that name with "missing" value&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, using the above 3, think how can we translate that into SAS syntax. Gradually, you will build the code. Try&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 12:16:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589327#M168545</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-17T12:16:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589334#M168546</link>
      <description>&lt;P&gt;You might want to just use normal SAS code instead of SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dis_entity_id ;
  set &amp;amp;library..&amp;amp;input_table ;
  format&amp;nbsp;ENTITY_ID $ENTITY. ;
  keep entity_id;
run;

proc sort data=dis_entity_id nodupkey;
  by entity;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 12:50:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589334#M168546</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-17T12:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589335#M168547</link>
      <description>&lt;P&gt;One way would be to make a view which creates the variable.&amp;nbsp; Something like below should work, if you don't mind side effect of changing the order of variables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vInputTable/view=vInputTable ;
  length Entity_ID $8 ;
  call missing(Entity_ID) ;
  set &amp;amp;library..&amp;amp;Input_table ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then read that view in your PROC SQL step.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's often easier to fix data structures rather than code around them.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 12:55:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589335#M168547</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2019-09-17T12:55:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589337#M168549</link>
      <description>&lt;P&gt;Proc sort ending with error if there is no value for ENTITY_ID. Yes, it is right, but I'd like tweak the sort code to work only if there is any Observation from the dataset 'dis_entity_id'. Your proposed Data step works well for both the cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;33         proc sort data=dis_entity_id nodupkey;
34           by entity;
ERROR: Variable ENTITY not found.
35         run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:09:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589337#M168549</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-17T13:09:30Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589338#M168550</link>
      <description>&lt;P&gt;Make sure to use the right variable name in the BY statement.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:15:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589338#M168550</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-17T13:15:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589350#M168557</link>
      <description>&lt;P&gt;You can check if the column exist with SQL, e.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  %let name=' ' as ENTITY_ID;
  select name into :name from
  dictionary.columns 
  where libname="%upcase(&amp;amp;library)" and memname="%upcase(&amp;amp;Input_table)" and name='ENTITY_ID';
  create table dis_entity_id as select distinct &amp;amp;name format=$ENTITY. from &amp;amp;library..&amp;amp;Input_table.;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's a bit fast and dirty, but I think it should work. The idea is that if the column is not found, the macro variable NAME will not be overwritten.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:31:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589350#M168557</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-09-17T13:31:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589359#M168560</link>
      <description>&lt;P&gt;You don't mind the uninitialized note thrown by:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dis_entity_id ;
  set &amp;amp;library..&amp;amp;input_table ;
  format ENTITY_ID $ENTITY. ;
  keep entity_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Generally I tend to consider that an error message.&amp;nbsp; (Actually, I tend to make it an error message with&amp;nbsp;&lt;SPAN&gt;VARINITCHK=error).&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:36:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589359#M168560</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2019-09-17T13:36:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create new variable if don't exists</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589364#M168563</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/19879"&gt;@Quentin&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;You don't mind the uninitialized note thrown by:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dis_entity_id ;
  set &amp;amp;library..&amp;amp;input_table ;
  format ENTITY_ID $ENTITY. ;
  keep entity_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Generally I tend to consider that an error message.&amp;nbsp; (Actually, I tend to make it an error message with&amp;nbsp;&lt;SPAN&gt;VARINITCHK=error).&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Nope. It is useful information.&amp;nbsp; It lets us know that ENTITY_ID did not exist in the source data.&lt;/P&gt;</description>
      <pubDate>Tue, 17 Sep 2019 13:48:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-new-variable-if-don-t-exists/m-p/589364#M168563</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-09-17T13:48:14Z</dc:date>
    </item>
  </channel>
</rss>

