<?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: Using Let statements conditionally in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456635#M115677</link>
    <description>&lt;P&gt;Macro code is used to generate SAS code. By the time the SAS code starts running the macro code has been run already.&lt;/P&gt;
&lt;P&gt;So you are running these commands.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yyyymm = 201801;
%let yr = %sysfunc(substr(&amp;amp;yyyymm,1,4));
%let mth = %sysfunc(substr(&amp;amp;yyyymm,5,2));
%let pyyyymm = cats(&amp;amp;yr.-1, 12);
%let pyyyymm = &amp;amp;yyyymm.-1;

data null_;
  if "&amp;amp;mth" = '01' then do; end;
  else do; end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to use your data step to generate macro variables then use the CALL SYMPUTX() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data null_;
  if "&amp;amp;mth" = '01' then call symputx('pyyyymm',cats(&amp;amp;yr.-1, 12));
  else call symputx('pyyyymm',&amp;amp;yyyymm.-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course if you are just trying to subtract one month from a date then convert your strings into dates and use functions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let pyyyymm=%sysfunc(intnx(month,%sysfunc(inputn(&amp;amp;yyyymm.01,yymmdd8)),-1),yymmn6);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 Apr 2018 22:56:13 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-04-23T22:56:13Z</dc:date>
    <item>
      <title>Using Let statements conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456631#M115674</link>
      <description>&lt;P&gt;Hi I'm having trouble getting the answer to a very trivial problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to get my previous date (year and month) to account for the flip of the year scenario.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;yyyymm = 201805&lt;/P&gt;&lt;P&gt;pyyyymm = 201804&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;yyyymm = 201801&lt;/P&gt;&lt;P&gt;pyyyymm = 201712&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But the following program just spews out 201800 for when yyyymm = 201801...and i cant figure out why?&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let yyyymm = 201801;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let yr = %sysfunc(substr(&amp;amp;yyyymm,1,4));&lt;BR /&gt;%let mth = %sysfunc(substr(&amp;amp;yyyymm,5,2));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data null_;&lt;BR /&gt;if "&amp;amp;mth" = '01' then do;&lt;BR /&gt;%let pyyyymm = cats(&amp;amp;yr.-1, 12);&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;%let pyyyymm = &amp;amp;yyyymm.-1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Apr 2018 18:27:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456631#M115674</guid>
      <dc:creator>waldo11</dc:creator>
      <dc:date>2018-04-23T18:27:28Z</dc:date>
    </item>
    <item>
      <title>Re: Using Let statements conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456634#M115676</link>
      <description>&lt;P&gt;Are you looking to do something like this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro waldo11;
 

%let yyyymm = 201801;

 

%let yr = %sysfunc(substr(&amp;amp;yyyymm,1,4));
%let mth = %sysfunc(substr(&amp;amp;yyyymm,5,2));

 

%if &amp;amp;mth = 01 %then %do;
%let pyyyymm = %sysfunc(cats(&amp;amp;yr.-1, 12));
%end;
%else %do;
%let pyyyymm = %val(&amp;amp;yyyymm.-1);
%end;

%put pyyyymm=&amp;amp;pyyyymm;
%mend waldo11;

%waldo11&lt;BR /&gt;&lt;BR /&gt;/*or*/&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;%let yyyymm = 201801;&lt;BR /&gt;&lt;BR /&gt; &lt;BR /&gt;&lt;BR /&gt;%let yr = %sysfunc(substr(&amp;amp;yyyymm,1,4));&lt;BR /&gt;%let mth = %sysfunc(substr(&amp;amp;yyyymm,5,2));&lt;BR /&gt;data _null_;&lt;BR /&gt;if "&amp;amp;mth" = '01' then do;&lt;BR /&gt;call symput( 'pyyyymm',cats(&amp;amp;yr.-1, 12));&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;call symput('pyyyymm',&amp;amp;yyyymm.-1);&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;=pyyyymm;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Apr 2018 18:45:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456634#M115676</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-23T18:45:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using Let statements conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456635#M115677</link>
      <description>&lt;P&gt;Macro code is used to generate SAS code. By the time the SAS code starts running the macro code has been run already.&lt;/P&gt;
&lt;P&gt;So you are running these commands.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let yyyymm = 201801;
%let yr = %sysfunc(substr(&amp;amp;yyyymm,1,4));
%let mth = %sysfunc(substr(&amp;amp;yyyymm,5,2));
%let pyyyymm = cats(&amp;amp;yr.-1, 12);
%let pyyyymm = &amp;amp;yyyymm.-1;

data null_;
  if "&amp;amp;mth" = '01' then do; end;
  else do; end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to use your data step to generate macro variables then use the CALL SYMPUTX() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data null_;
  if "&amp;amp;mth" = '01' then call symputx('pyyyymm',cats(&amp;amp;yr.-1, 12));
  else call symputx('pyyyymm',&amp;amp;yyyymm.-1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course if you are just trying to subtract one month from a date then convert your strings into dates and use functions.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let pyyyymm=%sysfunc(intnx(month,%sysfunc(inputn(&amp;amp;yyyymm.01,yymmdd8)),-1),yymmn6);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Apr 2018 22:56:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456635#M115677</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-04-23T22:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using Let statements conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456640#M115682</link>
      <description>&lt;P&gt;Definitely, as&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;said, you should be using SAS dates to do the calculations and the INTNX function to do this.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Don't try to do math on human readable dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;If you need to convert the results to a human readable format (which SAS dates are not), then when you do the output, apply the proper format.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Apr 2018 18:48:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456640#M115682</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2018-04-23T18:48:21Z</dc:date>
    </item>
    <item>
      <title>Re: Using Let statements conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456642#M115683</link>
      <description>Hi:&lt;BR /&gt;  You are mixing DATA step logic with creating a Macro variable. If you want to create a macro variable conditionally, then your choices are &lt;BR /&gt;1) to write a macro program definition and use macro logic with %IF or &lt;BR /&gt;2) to write a DATA step program and use CALL SYMPUT or CALL SYMPUTX.&lt;BR /&gt;&lt;BR /&gt;When you use a standard DATA step program the %LET is resolved at compile time, not at execution time, so the results will not really execute conditionally, as you envision because the %let statements are not really part of the program anymore, after compile time.&lt;BR /&gt;&lt;BR /&gt;Cynthia</description>
      <pubDate>Mon, 23 Apr 2018 18:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456642#M115683</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2018-04-23T18:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using Let statements conditionally</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456652#M115687</link>
      <description>A lot more clear now - thanks everyone!</description>
      <pubDate>Mon, 23 Apr 2018 19:09:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-Let-statements-conditionally/m-p/456652#M115687</guid>
      <dc:creator>waldo11</dc:creator>
      <dc:date>2018-04-23T19:09:50Z</dc:date>
    </item>
  </channel>
</rss>

