<?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 Conditionally Include Text in a Macro Variable Assignment in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521664#M141555</link>
    <description>&lt;P&gt;I have written&amp;nbsp;a program (that gets copied from one study to another) that&amp;nbsp;specifies a list of ID variables that will be used throughout several subprograms.&amp;nbsp;&amp;nbsp;I also have a couple of binary variables that are created by searching a table for the existence of a specified variable.&amp;nbsp; If it exists, I want it included in the ID variable list; otherwise, it should be excluded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;STUDYPART_EXIST=0 if STUDYPART does not exist and 1 if it does.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BATCH_EXIST=0 if BATCH does not exist and 1 if it does.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a section of code that checks all 4 possible combinations of a variable existing to see if it is properly storing the necessary variable in the macro variable IDVARS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro idvars (studypart_exist=, batch_exist=);
	%if &amp;amp;stdypart_exist.=1 &amp;amp; &amp;amp;batch_exist.=1 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR STDYPART BATCH AESPID AETERM AESTDATC AEENDATC);
	%end;
	%if &amp;amp;stdypart_exist.=1 &amp;amp; &amp;amp;batch_exist.=0 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR STDYPART AESPID AETERM AESTDATC AEENDATC);
	%end;
	%if &amp;amp;stdypart_exist.=0 &amp;amp; &amp;amp;batch_exist.=1 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR BATCH AESPID AETERM AESTDATC AEENDATC);
	%end;
	%if &amp;amp;stdypart_exist.=0 &amp;amp; &amp;amp;batch_exist.=1 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR AESPID AETERM AESTDATC AEENDATC);
	%end;
        %put &amp;amp;idvars.;&lt;BR /&gt;%mend idvars;

%idvars (studypart_exist=0, batch_exist=0);
%idvars (studypart_exist=0, batch_exist=1);
%idvars (studypart_exist=1, batch_exist=0);
%idvars (studypart_exist=1, batch_exist=1);
&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;
&lt;P&gt;I am fine using my original code, but just out of curiosity (and in an attempt to continue learning something new) is there a way to shorten my code?&amp;nbsp; If I had to conditionally include a new variable, then my code includes 8 combinations.&amp;nbsp; Another one would mean 16, etc.&amp;nbsp; It would be nice to keep this simple and just have a single line per conditional variable if possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I decided to try&amp;nbsp;to use IF statements, but those are considered text and not IF statements.&amp;nbsp; Is there a way to simplify what I'm already doing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro idvars (studypart_exist=, batch_exist=);
	%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR)
				%if &amp;amp;stdypart_exist.=1 %then %do; STDYPART %end;
				%if &amp;amp;batch_exist.=1 %then %do; BATCH %end;
				%str(AESPID AETERM AESTDATC AEENDATC);
	%put &amp;amp;idvars.;
%mend idvars;

%idvars (studypart_exist=0, batch_exist=0);
%idvars (studypart_exist=0, batch_exist=1);
%idvars (studypart_exist=1, batch_exist=0);
%idvars (studypart_exist=1, batch_exist=1);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Dec 2018 21:50:16 GMT</pubDate>
    <dc:creator>djbateman</dc:creator>
    <dc:date>2018-12-14T21:50:16Z</dc:date>
    <item>
      <title>Conditionally Include Text in a Macro Variable Assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521664#M141555</link>
      <description>&lt;P&gt;I have written&amp;nbsp;a program (that gets copied from one study to another) that&amp;nbsp;specifies a list of ID variables that will be used throughout several subprograms.&amp;nbsp;&amp;nbsp;I also have a couple of binary variables that are created by searching a table for the existence of a specified variable.&amp;nbsp; If it exists, I want it included in the ID variable list; otherwise, it should be excluded.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;STUDYPART_EXIST=0 if STUDYPART does not exist and 1 if it does.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;BATCH_EXIST=0 if BATCH does not exist and 1 if it does.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a section of code that checks all 4 possible combinations of a variable existing to see if it is properly storing the necessary variable in the macro variable IDVARS:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro idvars (studypart_exist=, batch_exist=);
	%if &amp;amp;stdypart_exist.=1 &amp;amp; &amp;amp;batch_exist.=1 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR STDYPART BATCH AESPID AETERM AESTDATC AEENDATC);
	%end;
	%if &amp;amp;stdypart_exist.=1 &amp;amp; &amp;amp;batch_exist.=0 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR STDYPART AESPID AETERM AESTDATC AEENDATC);
	%end;
	%if &amp;amp;stdypart_exist.=0 &amp;amp; &amp;amp;batch_exist.=1 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR BATCH AESPID AETERM AESTDATC AEENDATC);
	%end;
	%if &amp;amp;stdypart_exist.=0 &amp;amp; &amp;amp;batch_exist.=1 %then %do;
		%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR AESPID AETERM AESTDATC AEENDATC);
	%end;
        %put &amp;amp;idvars.;&lt;BR /&gt;%mend idvars;

%idvars (studypart_exist=0, batch_exist=0);
%idvars (studypart_exist=0, batch_exist=1);
%idvars (studypart_exist=1, batch_exist=0);
%idvars (studypart_exist=1, batch_exist=1);
&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;
&lt;P&gt;I am fine using my original code, but just out of curiosity (and in an attempt to continue learning something new) is there a way to shorten my code?&amp;nbsp; If I had to conditionally include a new variable, then my code includes 8 combinations.&amp;nbsp; Another one would mean 16, etc.&amp;nbsp; It would be nice to keep this simple and just have a single line per conditional variable if possible.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I decided to try&amp;nbsp;to use IF statements, but those are considered text and not IF statements.&amp;nbsp; Is there a way to simplify what I'm already doing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro idvars (studypart_exist=, batch_exist=);
	%let idvars=%str(SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR)
				%if &amp;amp;stdypart_exist.=1 %then %do; STDYPART %end;
				%if &amp;amp;batch_exist.=1 %then %do; BATCH %end;
				%str(AESPID AETERM AESTDATC AEENDATC);
	%put &amp;amp;idvars.;
%mend idvars;

%idvars (studypart_exist=0, batch_exist=0);
%idvars (studypart_exist=0, batch_exist=1);
%idvars (studypart_exist=1, batch_exist=0);
%idvars (studypart_exist=1, batch_exist=1);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 21:50:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521664#M141555</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-12-14T21:50:16Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Include Text in a Macro Variable Assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521679#M141560</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/74"&gt;@djbateman&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How about using macro variables of the form&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let batch=BATCH; /* if it exists */
%let batch=;      /* otherwise */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;instead of BATCH_EXIST etc.?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then you could simply define&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let idvars=SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR &amp;amp;STDYPART &amp;amp;BATCH AESPID AETERM AESTDATC AEENDATC;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(no macro IDVARS needed) and the non-existing variables would disappear automatically from the list (leaving a double blank, which you could compress if necessary, e.g. with &lt;A href="https://documentation.sas.com/?docsetId=mcrolref&amp;amp;docsetTarget=n0tvdbcgr9xc6dn14wmx9hpd6h51.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_blank"&gt;%CMPRES&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you needed the 0 or 1 from the former xxx_EXIST variables in other places of the program, you could easily generate them, e.g., in the form&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%eval(&amp;amp;batch ne)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Dec 2018 22:46:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521679#M141560</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-12-14T22:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Include Text in a Macro Variable Assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521692#M141563</link>
      <description>&lt;P&gt;Just use multiple %LET statements.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro idvars (studypart_exist=, batch_exist=);
%let idvars=SITECOUNTRY SITEMNEMONIC SUBJECTNUMBERSTR;
%if &amp;amp;stdypart_exist %then %let idvars=&amp;amp;idvars STDYPART;
%if &amp;amp;batch_exist %then %let idvars=&amp;amp;idvars BATCH;
%let idvars=&amp;amp;idvars AESPID AETERM AESTDATC AEENDATC;
%put &amp;amp;idvars.;
%mend idvars;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;PS Why were you adding macro quoting to characters that do not (and could not) contain anything that needs quoting?&lt;/P&gt;</description>
      <pubDate>Sat, 15 Dec 2018 01:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521692#M141563</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-15T01:28:48Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Include Text in a Macro Variable Assignment</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521912#M141641</link>
      <description>&lt;P&gt;Oh&amp;nbsp;my goodness!&amp;nbsp; How simple could it have been?&amp;nbsp; I feel kind of ridiculous for even posting my question now.&amp;nbsp; But thank you for showing me what I should have already known.&amp;nbsp; Thanks!&lt;/P&gt;</description>
      <pubDate>Mon, 17 Dec 2018 13:57:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Include-Text-in-a-Macro-Variable-Assignment/m-p/521912#M141641</guid>
      <dc:creator>djbateman</dc:creator>
      <dc:date>2018-12-17T13:57:16Z</dc:date>
    </item>
  </channel>
</rss>

