<?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: Check to see if variable exits and if not create variable with a value of 0 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882623#M348705</link>
    <description>&lt;P&gt;I cannot tell what you are asking for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Subject seems to be asking how to default variables to zero.&amp;nbsp; For that you could simply use a data step with a RETAIN statement.&amp;nbsp; To make it easier you might use an ARRAY also. For example if there are four variables that must exist and if they don't exist you want to create them with a value of zero you could do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class;
  array must_have CCE CCP CDI AGE  (4*0);
  retain CCE CCP CDI AGE ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1687871192470.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85413i4370166538E4BC9A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1687871192470.png" alt="Tom_0-1687871192470.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the rest of the question makes it sound like instead the question is how to test for the existence of the variables and return a list of those that are missing.&amp;nbsp; So if you have a dataset with the list of variables, like the list you showed, then just compare that to the list of variable that do exist and select the ones that are not found.&lt;/P&gt;
&lt;P&gt;Let's&amp;nbsp; store the list of variable names in a variable named NAME in a dataset named EXPECT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data expect ;
  input name $32.;
cards;
CCE
CCP
CDI
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we just need to get the list of names of the variables in the input dataset, let's call that HAVE, and generate a dataset with the list of missing names, let's call that WANT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=contents noprint; run;
proc sql;
create table want as 
select upcase(name) as name from expect
except 
select upcase(name) as name from contents
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 27 Jun 2023 13:26:10 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-06-27T13:26:10Z</dc:date>
    <item>
      <title>Check to see if variable exits and if not create variable with a value of 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882577#M348678</link>
      <description>&lt;P&gt;Is there a way to check to see if variable exists and if not create those variable and return a value of 0 for that.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is a list of the variables I'l like to check to see if they exist.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have daily data and depending on the day, the dataset could be 1,000 rows or down to 25 rows.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Will only need to return 1 row for each variable that does not exist.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;CCE
CCP
CDI
CHR
COR
CRC
DCS
DOH
IMM
INT
IQI
MAI
PHO
PRC
PRI
STA
OFF
ONL
ORE
P10
PAA
PPM
PTE
RSI
STP
TIP
TOR
WPM
WSM
WTE
WWW
YEC&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jun 2023 04:34:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882577#M348678</guid>
      <dc:creator>Haydo</dc:creator>
      <dc:date>2023-06-27T04:34:04Z</dc:date>
    </item>
    <item>
      <title>Re: Check to see if variable exits and if not create variable with a value of 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882582#M348681</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input myVarName $;
datalines;
CCE
CCP
CDI
CHR
COR
CRC
DCS
DOH
IMM
INT
IQI
MAI
PHO
PRC
PRI
STA
OFF
ONL
ORE
P10
PAA
PPM
PTE
RSI
STP
TIP
TOR
WPM
WSM
WTE
WWW
YEC
;
run;

data test;
   length CCE DCS YEC $10;
   call missing(CCE, DCS, YEC);
   delete;
run;


PROC SQL;
   CREATE TABLE want AS
      SELECT *
      FROM have
      WHERE upcase(myVarName) NOT IN
          (SELECT NAME
           FROM sashelp.vcolumn
           WHERE libname eq 'WORK'
             AND memname eq 'TEST')
   ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jun 2023 17:37:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882582#M348681</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2023-06-27T17:37:31Z</dc:date>
    </item>
    <item>
      <title>Re: Check to see if variable exits and if not create variable with a value of 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882603#M348692</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input have $;
cards;
CCE
CCP
CDI
CHR
COR
CRC
DCS
DOH
SEX
AGE
;

data x;
 set sashelp.class;
run;


proc transpose data=x(obs=0) out=temp;
var _all_;
run;

proc sql noprint;
select name into :vnames separated by ' ' 
from
(
select upcase(have) as name from have
except
select upcase(_name_) from temp
)
quit;
data want;
 set x;
 retain &amp;amp;vnames. 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 27 Jun 2023 11:37:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882603#M348692</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-06-27T11:37:58Z</dc:date>
    </item>
    <item>
      <title>Re: Check to see if variable exits and if not create variable with a value of 0</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882623#M348705</link>
      <description>&lt;P&gt;I cannot tell what you are asking for.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Subject seems to be asking how to default variables to zero.&amp;nbsp; For that you could simply use a data step with a RETAIN statement.&amp;nbsp; To make it easier you might use an ARRAY also. For example if there are four variables that must exist and if they don't exist you want to create them with a value of zero you could do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set sashelp.class;
  array must_have CCE CCP CDI AGE  (4*0);
  retain CCE CCP CDI AGE ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1687871192470.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/85413i4370166538E4BC9A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1687871192470.png" alt="Tom_0-1687871192470.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But the rest of the question makes it sound like instead the question is how to test for the existence of the variables and return a list of those that are missing.&amp;nbsp; So if you have a dataset with the list of variables, like the list you showed, then just compare that to the list of variable that do exist and select the ones that are not found.&lt;/P&gt;
&lt;P&gt;Let's&amp;nbsp; store the list of variable names in a variable named NAME in a dataset named EXPECT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data expect ;
  input name $32.;
cards;
CCE
CCP
CDI
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now we just need to get the list of names of the variables in the input dataset, let's call that HAVE, and generate a dataset with the list of missing names, let's call that WANT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc contents data=have out=contents noprint; run;
proc sql;
create table want as 
select upcase(name) as name from expect
except 
select upcase(name) as name from contents
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 27 Jun 2023 13:26:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-to-see-if-variable-exits-and-if-not-create-variable-with-a/m-p/882623#M348705</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-06-27T13:26:10Z</dc:date>
    </item>
  </channel>
</rss>

