<?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  Var  with null value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794033#M254566</link>
    <description>&lt;P&gt;When you have a WHERE clause in the query PROC SQL might not ever write anything into the target macro variable.&lt;/P&gt;
&lt;P&gt;In general it is easiest to set a default value before the query.&lt;/P&gt;
&lt;P&gt;Or you can test the automatic macro variable SQLOBS to see if anything was found.&lt;/P&gt;
&lt;P&gt;You also should remove the COMPRES() function calls. Is your data that messy?&amp;nbsp; To make the generated macro variable shorter use TRIM() function instead to remove any trailing spaces that are stored in the fixed length character variables in the datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt;
  input ID Make $ Type $ price $;
cards;
1 Audi  Sedan 100000
2 Audi  Sports 200000
3 Tesla Sports 150000
;

%let make='Tesla';
proc sql noprint;
%let v2=;
select distinct quote(trim(type), "'") into :V2  separated by ','
  from sashelp.cars
  where make=&amp;amp;make.
;
%put &amp;amp;=sqlobs &amp;amp;=v2;
%if (&amp;amp;sqlobs) %then %do ;
create table wanted as
  select *
  from  ttt
  where  Type in (&amp;amp;v2.)
;
%end;
%else %do;
%put No types found for &amp;amp;=make. ;
%end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 02 Feb 2022 15:09:58 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-02-02T15:09:58Z</dc:date>
    <item>
      <title>Macro  Var  with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/793987#M254545</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;User run this code 2 times:&lt;/P&gt;
&lt;P&gt;First time with %let make=Audi&lt;/P&gt;
&lt;P&gt;Second time with %let make=Tesla&lt;/P&gt;
&lt;P&gt;I have some questions:&lt;/P&gt;
&lt;P&gt;1-When user run make=&lt;CODE class=" language-sas"&gt;'Audi' &amp;nbsp;then&amp;nbsp;V2='Sedan','Sports','Wagon'&amp;nbsp;and&amp;nbsp;then&amp;nbsp;I&amp;nbsp;expect&amp;nbsp;to&amp;nbsp;get&amp;nbsp;rows&amp;nbsp;in&amp;nbsp;dta&amp;nbsp;set&amp;nbsp;wanted.&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Why&amp;nbsp;do&amp;nbsp;I&amp;nbsp;get&amp;nbsp;a&amp;nbsp;data&amp;nbsp;set&amp;nbsp;with&amp;nbsp;no&amp;nbsp;rows?&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;2-when&amp;nbsp;user&amp;nbsp;run&amp;nbsp;make='Tesla'&amp;nbsp;then&amp;nbsp;V2&amp;nbsp;will&amp;nbsp;have&amp;nbsp;null&amp;nbsp;value&amp;nbsp;and&amp;nbsp;then&amp;nbsp;I&amp;nbsp;want&amp;nbsp;to&amp;nbsp;stop&amp;nbsp;the&amp;nbsp;process&amp;nbsp;.&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;What&amp;nbsp;is&amp;nbsp;the&amp;nbsp;way&amp;nbsp;to&amp;nbsp;do&amp;nbsp;it&amp;nbsp;please?&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%let make='Audi';
/*%let make='Tesla';*/

Data ttt;
Input ID Type $ price $;
cards;
1 Audi Sedan 100000
2 Audi Sports 200000
3 Tesla Sports 150000
;
Run;

proc sql noprint;
select distinct compress(quote(type, "'")) into :V2  separated by ","
from sashelp.cars
where make=&amp;amp;make.
;
quit;
%put &amp;amp;v2;

IF %v2.  ne '' then do;
proc sql;
create table wanted as
select *
from  ttt
where  compress(Type) in (&amp;amp;v2.)
;
quit;
else Do nothing&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 11:43:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/793987#M254545</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-02-02T11:43:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro  Var  with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/793988#M254546</link>
      <description>&lt;P&gt;When you run the first PROC SQL, you are accessing a data set that does not contain any Tesla (which is what SAS is telling you). With that hint, you should be able to figure it out yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Debugging tip: when you get unexpected results, LOOK AT the data set being used with your own eyes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please do not send us code that you haven't debugged for OBVIOUS errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF %v2.  ne '' then do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;will never work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please do not send us code that you haven't debugged for OBVIOUS errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;quit;
else Do nothing&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;isn't even SAS code.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 12:11:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/793988#M254546</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-02T12:11:04Z</dc:date>
    </item>
    <item>
      <title>Re: Macro  Var  with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/793990#M254547</link>
      <description>&lt;P&gt;Maxim 2: Read the Log (and do it with diligence!)&lt;/P&gt;
&lt;PRE&gt; 69         %let make='Tesla';
 70         
 71         proc sql noprint;
 72         select distinct compress(quote(type, "'")) into :V2  separated by ","
 73         from sashelp.cars
 74         where make=&amp;amp;make.
 75         ;
 &lt;FONT color="#FF0000"&gt;NOTE: No rows were selected.
&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;When "No rows were selected." happens, SQL does not (re)create the macro variable.&lt;/P&gt;
&lt;P&gt;You need to create a "default" value first, and react on that value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let make=Tesla;

data ttt;
Input ID Type $ price $;
cards;
1 Audi Sedan 100000
2 Audi Sports 200000
3 Tesla Sports 150000
;

%let v2 =;

proc sql noprint;
select distinct compress(quote(type, "'")) into :V2  separated by ","
from sashelp.cars
where make="&amp;amp;make."
;
quit;

%IF &amp;amp;v2. ne %then %do;
proc sql;
create table wanted as
select *
from ttt
where compress(Type) in (&amp;amp;v2.)
;
quit;
%end;
%else %do;
%put No cars for this manufacturer!;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 12:00:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/793990#M254547</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-02T12:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: Macro  Var  with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794010#M254554</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;There are two issues with your code.&lt;BR /&gt;First you have enclosed Tesla in single quotes. So the values of make will be&lt;FONT color="#000080"&gt;&lt;STRONG&gt; 'Tesla'&lt;/STRONG&gt;&lt;/FONT&gt; and not &lt;FONT color="#000080"&gt;&lt;STRONG&gt;Tesla&lt;/STRONG&gt;&lt;/FONT&gt;.&lt;BR /&gt;Second in the Proc sql you have used&lt;FONT color="#333399"&gt;&lt;STRONG&gt; where make=&amp;amp;make&lt;/STRONG&gt;&lt;/FONT&gt;. As values of macro variables are of the type character, you must enclose in double quotes. Thus it should be &lt;STRONG&gt;&lt;FONT color="#000080"&gt;where make="&amp;amp;make" . &lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT color="#000000"&gt;The code given by&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT color="#000000"&gt;is the proper way to do it.&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;&lt;BR /&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 13:52:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794010#M254554</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-02-02T13:52:29Z</dc:date>
    </item>
    <item>
      <title>Macro var with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794012#M254563</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;The macro variable&amp;nbsp; V1 should contain nothing (null) because there is no Tesla cars in sashelp.cars&lt;/P&gt;
&lt;P&gt;When I run this code I get warning&lt;/P&gt;
&lt;P&gt;35 %put &amp;amp;V1;&lt;BR /&gt;WARNING: Apparent symbolic reference V1 not resolved.&lt;BR /&gt;&amp;amp;V1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the way to solve it and get null value into this macro variable?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let make='Tesla';
proc sql noprint;
select distinct compress(quote(upcase(type), "'")) into :V1  separated by ","
from sashelp.cars
where make=&amp;amp;make.
;
quit;
%put &amp;amp;V1;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Feb 2022 14:05:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794012#M254563</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-02-02T14:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro var with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794015#M254564</link>
      <description>&lt;P&gt;Try assigning a null value first. Then, if there are no matching records found by PROC SQL, the macro variable still has a value. (Otherwise, as you have seen, the macro variable doesn't exist)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let v1=;
%let make=Tesla;
proc sql noprint;
select distinct compress(quote(upcase(type), "'")) into :V1  separated by ","
from sashelp.cars
where make="&amp;amp;make."
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Feb 2022 14:11:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794015#M254564</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-02-02T14:11:48Z</dc:date>
    </item>
    <item>
      <title>Re: Macro  Var  with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794017#M254560</link>
      <description>&lt;P&gt;Great,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let make=Audi;
/*%let make=Tesla;*/

%put &amp;amp;make; 
data ttt;
Input ID make $ Type $  price;
cards;
1 Audi Sedan 100000
2 Audi Sports 200000
3 Tesla Sports 150000
;
quit;


/*You need to create a "default" value first*/
%let v2 =;
proc sql noprint;
select distinct compress(quote(type, "'")) into :V2  separated by ","
from sashelp.cars
where make="&amp;amp;make."
;
quit;
%put &amp;amp;v2; 

%Macro RRR;
%IF &amp;amp;v2. ne %then %do;
proc sql;
create table wanted as
select *
from ttt
where compress(Type) in (&amp;amp;v2.)
;
quit;
%end;
%else %do;
%put No cars for this manufacturer!;
%end;
%mend RRR;
%RRR;

 &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 14:20:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794017#M254560</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-02-02T14:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Macro  Var  with null value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794033#M254566</link>
      <description>&lt;P&gt;When you have a WHERE clause in the query PROC SQL might not ever write anything into the target macro variable.&lt;/P&gt;
&lt;P&gt;In general it is easiest to set a default value before the query.&lt;/P&gt;
&lt;P&gt;Or you can test the automatic macro variable SQLOBS to see if anything was found.&lt;/P&gt;
&lt;P&gt;You also should remove the COMPRES() function calls. Is your data that messy?&amp;nbsp; To make the generated macro variable shorter use TRIM() function instead to remove any trailing spaces that are stored in the fixed length character variables in the datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ttt;
  input ID Make $ Type $ price $;
cards;
1 Audi  Sedan 100000
2 Audi  Sports 200000
3 Tesla Sports 150000
;

%let make='Tesla';
proc sql noprint;
%let v2=;
select distinct quote(trim(type), "'") into :V2  separated by ','
  from sashelp.cars
  where make=&amp;amp;make.
;
%put &amp;amp;=sqlobs &amp;amp;=v2;
%if (&amp;amp;sqlobs) %then %do ;
create table wanted as
  select *
  from  ttt
  where  Type in (&amp;amp;v2.)
;
%end;
%else %do;
%put No types found for &amp;amp;=make. ;
%end;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Feb 2022 15:09:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-Var-with-null-value/m-p/794033#M254566</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-02T15:09:58Z</dc:date>
    </item>
  </channel>
</rss>

