<?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: Macro variable empty values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370020#M88377</link>
    <description>&lt;P&gt;check whether you have zero records and then only execute. something like this&lt;/P&gt;
&lt;PRE&gt;data abc;
a=2;
b=3;
run;

%macro abc;
proc sql noprint;
select count(*) into :cnt from 
(select distinct * from abc
where a ne 2)a; /*try with eq 2*/
quit;
%if %sysevalf(&amp;amp;cnt) = 0 %then %do;
%put "no values are there";
%end;
%else %do;
proc sql;
create table want as 
select distinct * from abc
;
quit;
%end;
%mend;
%abc;&lt;/PRE&gt;</description>
    <pubDate>Fri, 23 Jun 2017 17:13:30 GMT</pubDate>
    <dc:creator>kiranv_</dc:creator>
    <dc:date>2017-06-23T17:13:30Z</dc:date>
    <item>
      <title>Macro variable empty values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370011#M88372</link>
      <description>&lt;P&gt;proc sql;&lt;BR /&gt;select distinct Char_flag, num_flag into :charvars,:numvars separated by ","&lt;BR /&gt;from have&lt;BR /&gt;where Char_flag ne '' or num_flag ne '';&lt;BR /&gt;quit;&lt;BR /&gt;%put &amp;amp;charvars;&lt;BR /&gt;%put &amp;amp;numvars;&lt;/P&gt;&lt;P&gt;Macro variable numvars has no values in it. So when I pass in it in the below sql code I'm getting an error. How to pass it conditionally only if it has values in it?&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select &amp;amp;charvars&lt;BR /&gt;&amp;amp;numvars&lt;BR /&gt;from have&lt;BR /&gt;;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 16:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370011#M88372</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2017-06-23T16:47:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable empty values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370020#M88377</link>
      <description>&lt;P&gt;check whether you have zero records and then only execute. something like this&lt;/P&gt;
&lt;PRE&gt;data abc;
a=2;
b=3;
run;

%macro abc;
proc sql noprint;
select count(*) into :cnt from 
(select distinct * from abc
where a ne 2)a; /*try with eq 2*/
quit;
%if %sysevalf(&amp;amp;cnt) = 0 %then %do;
%put "no values are there";
%end;
%else %do;
proc sql;
create table want as 
select distinct * from abc
;
quit;
%end;
%mend;
%abc;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jun 2017 17:13:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370020#M88377</guid>
      <dc:creator>kiranv_</dc:creator>
      <dc:date>2017-06-23T17:13:30Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable empty values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370025#M88379</link>
      <description>&lt;P&gt;Perhaps I am misuderstanding the question as it is a little confusing, but I think your issue is how to generate a select statement in SQL when sometimes the list of variables is empty. &amp;nbsp;Is that right?&lt;/P&gt;
&lt;P&gt;The easist way is to use normal SAS code that uses space delimited lists of variables instead of PROC SQL code that requires all of those stupid commas then the empty lists don't cause syntax errors.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let requried=A B;
%let optional=;
data want ;
  set have ;
  keep &amp;amp;required &amp;amp;optional;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Another trick I like to do is build the list as space delimited list and then convert to comma delimited list.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let required=A B;
%let optional=;
%let varlist=&amp;amp;required &amp;amp;optional;
%let sqllist=%sysfunc(tranwrd(&amp;amp;varlist,%str( ),%str(,)));
proc sql;
  create table want as
   select &amp;amp;sqllist
   from have
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 17:17:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370025#M88379</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-23T17:17:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable empty values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370038#M88385</link>
      <description>&lt;P&gt;Are your variables Char_flag and Num_flag in the set Have actually the names of variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you need the separate charvars and numvars for something else later?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first thing I would do would be pull the lists separately:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;proc&lt;/FONT&gt; &lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;sql&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;select&lt;/FONT&gt; &lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;distinct&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; Char_flag &lt;/FONT&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;into&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; :charvars separated &lt;/FONT&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;by&lt;/FONT&gt; &lt;FONT color="#800080" face="SAS Monospace" size="2"&gt;","&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;from&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; have&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;where&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; not missing( Char_flag) ;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;select&lt;/FONT&gt; &lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;distinct&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; num_flag &lt;/FONT&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;into&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; :numvars separated &lt;/FONT&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;by&lt;/FONT&gt; &lt;FONT color="#800080" face="SAS Monospace" size="2"&gt;","&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;from&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; have&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;where&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; not missing( num_flag);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;quit&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;your compound requirement with the OR and Distinct may not be getting what you want to begin with.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 17:31:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-empty-values/m-p/370038#M88385</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-06-23T17:31:27Z</dc:date>
    </item>
  </channel>
</rss>

