<?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: How to do the coalesce of macro variable and today's date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826851#M326604</link>
    <description>&lt;P&gt;If you're not sure whether it will be replaced or not, you can test it out, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%symdel yrmonth /nowarn ;

data source_data ;
  yearmonth=202201 ;
  delete ;
run ;

%let yrmonth = %substr(%sysfunc(today(), yymmddn8.), 1, 6);;

proc sql;
  select put(yearmonth,6.) into  :yrmonth from source_data;
quit;

%put &amp;amp;yrmonth;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 03 Aug 2022 13:06:13 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2022-08-03T13:06:13Z</dc:date>
    <item>
      <title>How to do the coalesce of macro variable and today's date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826779#M326570</link>
      <description>&lt;P&gt;I have a program as below. Where I am getting value from the existing table. Incase existing table has no value how can I do the coalesce ?&amp;nbsp; Here in case source data has no values. I tried code below but it is not working. I have another non null value in a macro variable &amp;amp;yearmonth .&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
	select coalesce (put(max(yearmonth),6.) , &amp;amp;yearmonth)) into :  yrmonth from source_data;
quit;

proc sql;
	delete * from target_data
		where yearmonth= &amp;amp;yrmonth.;
quit;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 04:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826779#M326570</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2022-08-03T04:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to do the coalesce of macro variable and today's date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826781#M326572</link>
      <description>&lt;P&gt;You can't use COALESCE here as if there are no rows in SOURCE_DATA the SQL statement won't execute. Assign YRMONTH first, then overwrite it with your SQL if SOURCE_DATA is populated:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yrmonth = %substr(%sysfunc(today(), yymmddn8.), 1, 6);;
%put &amp;amp;yrmonth;

proc sql;
  select put(yearmonth,6.) into  :yrmonth from source_data;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 04:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826781#M326572</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-08-03T04:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to do the coalesce of macro variable and today's date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826791#M326577</link>
      <description>&lt;P&gt;You need to set a "default" before the SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yrmonth = &amp;amp;yearmonth;
proc sql noprint;
select put(max(yearmonth),6.) into :  yrmonth from source_data;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your SQL DELETE will not work because of a syntax error. Omit the asterisk.&lt;/P&gt;
&lt;P&gt;And it's not clear which type the variables in your datasets are, so you need to decide where to use quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 06:13:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826791#M326577</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-03T06:13:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to do the coalesce of macro variable and today's date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826836#M326593</link>
      <description>&lt;P&gt;Agree with others that assigning a default value is the way to do it.&amp;nbsp; But just as a thought exercise, you can use COALESCE via %sysfunc, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%symdel yrmonth /nowarn ;

data source_data ;
  yearmonth=202201 ;
  delete ;
run ;

options missing=' ' ;
proc sql;
	select put(max(yearmonth),6.) into :  yrmonth from source_data ;
quit;
options missing=. ;

%put &amp;gt;&amp;gt;&amp;amp;yrmonth&amp;lt;&amp;lt; ;

%let yrmonth=%sysfunc(CoalesceC(&amp;amp;yrmonth,%substr(%sysfunc(today(), yymmddn8.), 1, 6))) ;

%put &amp;gt;&amp;gt;&amp;amp;yrmonth&amp;lt;&amp;lt; ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 12:41:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826836#M326593</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-08-03T12:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to do the coalesce of macro variable and today's date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826850#M326603</link>
      <description>&lt;P&gt;This will give me a non- null value for sure&amp;nbsp;&lt;/P&gt;&lt;PRE class=""&gt;&lt;CODE&gt;%let yrmonth = %substr(%sysfunc(today(), yymmddn8.), 1, 6);;
%put &amp;amp;yrmonth;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;but I am wondering if it will be replaced with the null value in the next step if there are no records in the source data?&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please correct me if I am wrong.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Aug 2022 13:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826850#M326603</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2022-08-03T13:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to do the coalesce of macro variable and today's date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826851#M326604</link>
      <description>&lt;P&gt;If you're not sure whether it will be replaced or not, you can test it out, e.g.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%symdel yrmonth /nowarn ;

data source_data ;
  yearmonth=202201 ;
  delete ;
run ;

%let yrmonth = %substr(%sysfunc(today(), yymmddn8.), 1, 6);;

proc sql;
  select put(yearmonth,6.) into  :yrmonth from source_data;
quit;

%put &amp;amp;yrmonth;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 13:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826851#M326604</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-08-03T13:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to do the coalesce of macro variable and today's date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826861#M326608</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259983"&gt;@kajal_30&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;This will give me a non- null value for sure&amp;nbsp;&lt;/P&gt;
&lt;PRE class=""&gt;&lt;CODE&gt;%let yrmonth = %substr(%sysfunc(today(), yymmddn8.), 1, 6);;
%put &amp;amp;yrmonth;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but I am wondering if it will be replaced with the null value in the next step if there are no records in the source data?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please correct me if I am wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Depends on whether the SQL query returns any results or not.&amp;nbsp; For your simple query that depends on whether the dataset has any observations (even observations with missing values of the variable).&amp;nbsp; So you migth want to add a WHERE to your query to prevent it from overwriting the default value with a missing value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yrmonth=&amp;amp;yearmonth;
proc sql noprint;
select max(yearmonth) format=z6.
  into :yrmonth 
  from source_data
  where not missing(yearmonth)
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Aug 2022 13:20:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-the-coalesce-of-macro-variable-and-today-s-date/m-p/826861#M326608</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-03T13:20:32Z</dc:date>
    </item>
  </channel>
</rss>

