<?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: If then set statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596192#M171622</link>
    <description>&lt;P&gt;Note that the if conditions you have specified are all the same so it will be true for all or false for all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Further the if conditions appear to be relying on program (not macro) variables, but as no data sets have been read in and no variables have been initialised, the variables in the if conditions would have missing values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this what you intended?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
    <pubDate>Mon, 14 Oct 2019 09:38:19 GMT</pubDate>
    <dc:creator>Amir</dc:creator>
    <dc:date>2019-10-14T09:38:19Z</dc:date>
    <item>
      <title>If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596183#M171619</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was wondering if there was a way of conditioning a set statement. What I mean is adding an if to this set. I know it would be possible to do so outside of the set, but I'm having to set multiple tables at once...&lt;/P&gt;
&lt;P&gt;Here's my code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data last;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; set table1 table2 table3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; where var=&amp;amp;value.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what I'd like is something like this (if it exists...):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data last;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; set&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if var = value then; table1&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if var = value then; table2&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if var = value then; table3&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; where var2=&amp;amp;value2.;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 09:11:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596183#M171619</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-14T09:11:42Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596188#M171620</link>
      <description>&lt;P&gt;Please explain what you trying to achieve by posting some example observations of all used datasets. Also not that the declarative part of the set statement can't be excluded from execution at all. That's why&lt;/P&gt;
&lt;PRE&gt;if 0 then set wanted_vars; &lt;/PRE&gt;
&lt;P&gt;can be used to clone the structure of one dataset, but prevent the observations from being read.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 09:31:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596188#M171620</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-10-14T09:31:21Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596192#M171622</link>
      <description>&lt;P&gt;Note that the if conditions you have specified are all the same so it will be true for all or false for all.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Further the if conditions appear to be relying on program (not macro) variables, but as no data sets have been read in and no variables have been initialised, the variables in the if conditions would have missing values.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this what you intended?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 09:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596192#M171622</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2019-10-14T09:38:19Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596194#M171623</link>
      <description>&lt;P&gt;I didn't explain it very well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I'd like is that if the table exists, then I add it to the set statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;set&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; if (table1 exists) then table1&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; if (table2 exists) then table2&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; if (table3 exists) then table3&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 09:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596194#M171623</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-14T09:43:40Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596195#M171624</link>
      <description>&lt;P&gt;You have the right function name - &lt;FONT face="courier new,courier"&gt;exist()&lt;/FONT&gt;, as per the documentation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000210903.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000210903.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;HTH.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 09:51:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596195#M171624</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2019-10-14T09:51:12Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596197#M171625</link>
      <description>&lt;P&gt;wow thanks a lot!&lt;/P&gt;
&lt;P&gt;is it possible to include this in a set statement? if not how might i procede to set the table only if it exists knowing that i'm adding multiple tables to the set statement. would i have to use a PROC statement?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 10:07:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596197#M171625</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-14T10:07:44Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596201#M171626</link>
      <description>&lt;P&gt;There are more than one method to the EXIST function:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The short one is to use it within the macro function %SYSFUNC:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set
       %if %sysfunc(exist(table1)) %then %str(&amp;lt;table1_name&amp;gt;)
       %if %sysfunc(exist(table2)) %then %str(&amp;lt;table2_name&amp;gt;)
      ...
; run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Alternatively assign a macro variable with existing names and use it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
       length tables $30; /*adapt to max length need */
        if exist("&amp;lt;table1_name&amp;gt;") then tables = strip("&amp;lt;table1_name&amp;gt;");
        if exist("&amp;lt;table2_name&amp;gt;") then tables = 
                   catx(' ',tables,strip("&amp;lt;table2_name&amp;gt;"));
        if exist("&amp;lt;table3_name&amp;gt;") then tables = 
                   catx(' ',tables,strip("&amp;lt;table3_name&amp;gt;"));
       ....
       call symput('tables', strip(tables));
run;

data want;
   set &amp;amp;tables;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Oct 2019 10:28:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596201#M171626</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-10-14T10:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596202#M171627</link>
      <description>&lt;P&gt;In this case I would recommend creating a macro. For example the following code creates data sets have1 and have3 (not have2) and only uses the data sets that exist:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create have1 &amp;amp; have3, not have2 */
data have1 have3;
   set sashelp.class;
run;

/* define a macro to return the given data set name if it exists */
%macro use_ds_if_exists(ds);
   %if %sysfunc(exist(&amp;amp;ds)) %then
      &amp;amp;ds;
%mend use_ds_if_exists;

/* see what the macro generates when called */
options mprint;

/* invoke the macro in the data step */
data want;
   set
      %use_ds_if_exists(have1)
      %use_ds_if_exists(have2)
      %use_ds_if_exists(have3)
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;NB. Note that if none of the data sets exist then you have to consider what happens.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please remember to mark any solution posted you think works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Amir.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 10:29:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596202#M171627</guid>
      <dc:creator>Amir</dc:creator>
      <dc:date>2019-10-14T10:29:22Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596204#M171628</link>
      <description>&lt;P&gt;hello and thanks for the response!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i've tried your first method as it ressembles more to what i was trying at first but i get the following error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: Expected %DO not found.&lt;BR /&gt;ERROR: Skipping to next %END statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA combined;
	set
		%if %sysfunc(exist(table1)) %then %str (table1)
		%if %sysfunc(exist(table2)) %then %str (table2)
	;
	where year = &amp;amp;year.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Oct 2019 10:35:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596204#M171628</guid>
      <dc:creator>polpel</dc:creator>
      <dc:date>2019-10-14T10:35:41Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596205#M171629</link>
      <description>&lt;P&gt;Or use CALL EXECUTE:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
length dsn $32;
call execute('data last; set');
do dsn='table1', 'table2', 'table3';
  call execute(ifc(exist(dsn),dsn,'_null_'));
end;
call execute('; where var2=&amp;amp;value2; run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Oct 2019 10:36:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596205#M171629</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2019-10-14T10:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596208#M171632</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/291423"&gt;@polpel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;wow thanks a lot!&lt;/P&gt;
&lt;P&gt;is it possible to include this in a set statement? if not how might i procede to set the table only if it exists knowing that i'm adding multiple tables to the set statement. would i have to use a PROC statement?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You could do something like here:&amp;nbsp;&lt;A href="http://support.sas.com/kb/24/670.html" target="_blank"&gt;http://support.sas.com/kb/24/670.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Code as below should also work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set
    %sysfunc(ifc(%sysfunc(exist(sashelp.class)),sashelp.class,))
    %sysfunc(ifc(%sysfunc(exist(sashelp.somethingelse)),sashelp.somethingelse,))
    indsname=_dsin;
    ;
    dsin=_dsin;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Oct 2019 10:50:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596208#M171632</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-10-14T10:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: If then set statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596261#M171653</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/291423"&gt;@polpel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;hello and thanks for the response!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i've tried your first method as it ressembles more to what i was trying at first but i get the following error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ERROR: Expected %DO not found.&lt;BR /&gt;ERROR: Skipping to next %END statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;regards&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA combined;
	set
		%if %sysfunc(exist(table1)) %then %str (table1)
		%if %sysfunc(exist(table2)) %then %str (table2)
	;
	where year = &amp;amp;year.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need a semicolon for each %IF statement:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA combined;
	set
		%if %sysfunc(exist(table1)) %then table1 ; /*semicolon ends the %IF statement*/
		%if %sysfunc(exist(table2)) %then table2 ; /*semicolon ends the %IF statement*/
	; /*semicolon ends the SET statement*/
	where year = &amp;amp;year.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks funny until your eyes get used to it, but the semicolons end the macro language %IF statements, they do not end the SET statement.&amp;nbsp; They are macro language semicolons, not DATA step language semicolons.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2019 13:13:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-then-set-statement/m-p/596261#M171653</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2019-10-14T13:13:27Z</dc:date>
    </item>
  </channel>
</rss>

