<?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: Checking which columns missing from table and adding them in in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726063#M28088</link>
    <description>&lt;P&gt;Use a retain statement in a datastep&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fruits;
input fruit$;
cards;
apple
orange
pear
banana
lemon
;

data have;
input apple orange banana lemon;
cards;
1 2 3 4
2 3 4 5
1 0 2 2
;

proc sql;
select fruit into :fruits separated by " "
from fruits
quit;

data want;
retain &amp;amp;fruits. (99*0);
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 13 Mar 2021 20:49:34 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2021-03-13T20:49:34Z</dc:date>
    <item>
      <title>Checking which columns missing from table and adding them in</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726061#M28087</link>
      <description>&lt;P&gt;Suppose I have 2 tables - Tables A and B.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*Table A*/
data work.table_a;
input fruits$;
cards;
apple
orange
pear
banana
lemon
;
run;
&lt;BR /&gt;/* Table B */
data work.table_b;
input apple orange banana lemon;
cards;
1 2 3 4
2 3 4 5
1 0 2 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Table A consists of just one column with 5 rows containing distinct string values. The number of rows and their values are fixed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Table B consists of columns with names that are a subset of the 5 rows of Table A. In this example, Table B consists of 4 columns and the value "pear" does not belong to this subset. However, this subset may change from time to time. For example, on another occasion when this table is generated, Table B may just have 3 columns, while the values missing from the subset might be "apple" and "banana".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am interested in finding out which are the values that are missing from the subset and subsequently, add them to Table B. The values under these columns will all be zeros.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Desired Output - column "pear" added to Table B */
data work.desired_output;
input apple orange pear banana lemon;
cards;
1 2 0 3 4
2 3 0 4 5
1 0 0 2 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Currently, I am using codes adopted from&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/macro-error-issue/td-p/715467" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/macro-error-issue/td-p/715467.&lt;/A&gt;Specifically, I am typing:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro VarExist(ds, var);
%local dsid resx rc;
%let dsid=%sysfunc(open(&amp;amp;ds));
%let resx=%sysfunc(varnum(&amp;amp;dsid, &amp;amp;var));
%let rc=%sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
%if &amp;amp;resx&amp;gt;0 %then %do;
%put &amp;amp;var exist;
%end;&lt;BR /&gt;
%else %do;
data &amp;amp;ds; set &amp;amp;ds;
&amp;amp;var=0;
run;
%end;&lt;BR /&gt;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%VarExist(table_b, apple);
%VarExist(table_b, orange);
%VarExist(table_b, pear);
%VarExist(table_b, banana);
%VarExist(table_b, lemon);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, such method may not be the most elegant as the real Table A that I am working on consists of 20+ rows. I would like to enquire if there is a better way to code this? Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Mar 2021 19:52:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726061#M28087</guid>
      <dc:creator>jlin4</dc:creator>
      <dc:date>2021-03-13T19:52:59Z</dc:date>
    </item>
    <item>
      <title>Re: Checking which columns missing from table and adding them in</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726063#M28088</link>
      <description>&lt;P&gt;Use a retain statement in a datastep&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fruits;
input fruit$;
cards;
apple
orange
pear
banana
lemon
;

data have;
input apple orange banana lemon;
cards;
1 2 3 4
2 3 4 5
1 0 2 2
;

proc sql;
select fruit into :fruits separated by " "
from fruits
quit;

data want;
retain &amp;amp;fruits. (99*0);
set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 Mar 2021 20:49:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726063#M28088</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-13T20:49:34Z</dc:date>
    </item>
    <item>
      <title>Re: Checking which columns missing from table and adding them in</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726064#M28089</link>
      <description>&lt;P&gt;If you want to have a fixed structure then just create that structure.&amp;nbsp; You could do it with code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length apple 8 banana 8 .... ;  
  set have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or with a dataset&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set expected(obs=0) have;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to fill the non-existent variables with zeros instead of missing you might want to use RETAIN.&amp;nbsp; Or if you also want to replace any missing in yoru actual data with zeros then perhaps wait and just replace them all with the same code.&lt;/P&gt;</description>
      <pubDate>Sat, 13 Mar 2021 20:56:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726064#M28089</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-03-13T20:56:11Z</dc:date>
    </item>
    <item>
      <title>Re: Checking which columns missing from table and adding them in</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726072#M28090</link>
      <description>thank you! may i ask what does "(99*0)" do?</description>
      <pubDate>Sun, 14 Mar 2021 04:50:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726072#M28090</guid>
      <dc:creator>jlin4</dc:creator>
      <dc:date>2021-03-14T04:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: Checking which columns missing from table and adding them in</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726123#M28094</link>
      <description>&lt;P&gt;99*0 is the initial values list for the retained variables. The list is too long, but that doesn't matter. Missing variables will thus be created with a value of zero, a value that will never change.&lt;/P&gt;</description>
      <pubDate>Sun, 14 Mar 2021 13:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Checking-which-columns-missing-from-table-and-adding-them-in/m-p/726123#M28094</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2021-03-14T13:24:40Z</dc:date>
    </item>
  </channel>
</rss>

