<?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: Extract Month Name from Month Number in a Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521945#M141655</link>
    <description>&lt;P&gt;Use the MONNAME format.&amp;nbsp; For that you need an actual date value, not a month number.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  now = today();
  current_month =intnx('month',now,-1,'b');
  previous_month = intnx('month',now,-2,'b');
  call symputx('cmonth',month(current_month),'g');
  call symputx('pmonth',month(previous_month),'g');
  call symputx('cyear',year(current_month),'g');
  call symputx('pyear',year(previous_month),'g');
  call symputx('cmonth_name',put(current_month,monname.),'g');
  call symputx('pmonth_name',put(previous_month,monname.),'g');
run;
%put &amp;amp;=cmonth &amp;amp;=cyear &amp;amp;=cmonth_name ;
%put &amp;amp;=pmonth &amp;amp;=pyear &amp;amp;=pmonth_name ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;12    %put &amp;amp;=cmonth &amp;amp;=cyear &amp;amp;=cmonth_name ;
CMONTH=11 CYEAR=2018 CMONTH_NAME=November
13    %put &amp;amp;=pmonth &amp;amp;=pyear &amp;amp;=pmonth_name ;
PMONTH=10 PYEAR=2018 PMONTH_NAME=October
&lt;/PRE&gt;</description>
    <pubDate>Mon, 17 Dec 2018 15:19:06 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-12-17T15:19:06Z</dc:date>
    <item>
      <title>Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521933#M141649</link>
      <description>&lt;P&gt;I have the following, which is used to setup variables throughout a project:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dateset;
	%global cmonth pmonth cyear pyear cmonth_name;
	%let sysmonth = %sysfunc(month("&amp;amp;sysdate"d));
	%let sysyear = %sysfunc(year("&amp;amp;sysdate"d));

	%if &amp;amp;sysmonth = 1 %then %do;
		%let cmonth = 12;
		%let cmonth_name = "December";
		%let pmonth = 11;
		%let cyear = %sysevalf(&amp;amp;sysyear - 1);
		%let pyear = &amp;amp;cyear;
	%end;
	%if &amp;amp;sysmonth = 2 %then %do;
		%let cmonth = 1;
		%let cmonth_name = "January";
		%let pmonth = 12;
		%let cyear = &amp;amp;sysyear;
		%let pyear = %sysevalf(&amp;amp;cyear - 1);
	%end;
	%if &amp;amp;sysmonth &amp;gt;= 3 %then %do;
		%let cmonth = %sysevalf(&amp;amp;sysmonth - 1);
		%let cmonth_name = ?????
		%let pmonth = %sysevalf(&amp;amp;sysmonth - 2);
		%let cyear = &amp;amp;sysyear;
		%let pyear = &amp;amp;cyear;
	%end;
%mend;
%dateset;


%put &amp;amp;cmonth &amp;amp;cyear &amp;amp;cmonth_name &amp;amp;pmonth &amp;amp;pyear;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am stuck with converting the month number in:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; cmonth = &lt;/FONT&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%sysevalf&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt;(&amp;amp;sysmonth - 1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000ff" face="Courier New" size="3"&gt;%let&lt;/FONT&gt;&lt;FONT face="Courier New" size="3"&gt; cmonth_name = ?????&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Probably easy for some here....but I am stuck.&amp;nbsp; Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 17 Dec 2018 14:53:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521933#M141649</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-12-17T14:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521936#M141651</link>
      <description>&lt;P&gt;Well, lets start with the usual.&amp;nbsp; Its not a good idea to create global macro variables inside code like that.&amp;nbsp; It can lead to tricky bugs.&lt;/P&gt;
&lt;P&gt;Second, why do you need these macro variables?&amp;nbsp; As you provide there, all of them are available from one - sysdate - so why bother creating all of those macro variables?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third, Base SAS is the programming language, use it always.&amp;nbsp; It will cover every situation, macro is just additional find/replace system.&lt;/P&gt;
&lt;P&gt;To illustrate the Base SAS route, and how much simpler it is, consider:&lt;/P&gt;
&lt;PRE&gt;data want;
  original_month=put(month("&amp;amp;sysdate."d),monname8.);
  original_year=year("&amp;amp;sysdate."d);
run;&lt;/PRE&gt;
&lt;P&gt;Simple code, to do simple tasks.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Dec 2018 15:00:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521936#M141651</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-12-17T15:00:01Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521941#M141652</link>
      <description>&lt;P&gt;These variables are used for datasteps, sql, and excel output creation.&amp;nbsp; It is often much easier to have these setup so that other users can see where they come from once. I know there are neater ways of doing things, but this is what has been asked of me for now.&lt;/P&gt;</description>
      <pubDate>Mon, 17 Dec 2018 15:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521941#M141652</guid>
      <dc:creator>BCNAV</dc:creator>
      <dc:date>2018-12-17T15:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521942#M141653</link>
      <description>&lt;P&gt;That is so easy to do in a data step that just looking at this macro code abomination makes my stomach hurt:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('cmonth',month(today()));
call symputx('cmonth_name',put(today(),monname.));
call symputx('pmonth',month(intnx('month',today(),-1)));
call symputx('cyear',year(today()));
call symputx('pyear',year(intnx('month',today(),-1)));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bottom line: do not abuse the macro preprocessor for handling data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;&lt;EM&gt;Edit: removed the custom format, as monname. can do it. And removed the unnecessary MONTH() function, as pointed out by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/67956"&gt;@malmario&lt;/a&gt;&amp;nbsp;&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Mar 2021 08:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521942#M141653</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-03-04T08:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521945#M141655</link>
      <description>&lt;P&gt;Use the MONNAME format.&amp;nbsp; For that you need an actual date value, not a month number.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  now = today();
  current_month =intnx('month',now,-1,'b');
  previous_month = intnx('month',now,-2,'b');
  call symputx('cmonth',month(current_month),'g');
  call symputx('pmonth',month(previous_month),'g');
  call symputx('cyear',year(current_month),'g');
  call symputx('pyear',year(previous_month),'g');
  call symputx('cmonth_name',put(current_month,monname.),'g');
  call symputx('pmonth_name',put(previous_month,monname.),'g');
run;
%put &amp;amp;=cmonth &amp;amp;=cyear &amp;amp;=cmonth_name ;
%put &amp;amp;=pmonth &amp;amp;=pyear &amp;amp;=pmonth_name ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;12    %put &amp;amp;=cmonth &amp;amp;=cyear &amp;amp;=cmonth_name ;
CMONTH=11 CYEAR=2018 CMONTH_NAME=November
13    %put &amp;amp;=pmonth &amp;amp;=pyear &amp;amp;=pmonth_name ;
PMONTH=10 PYEAR=2018 PMONTH_NAME=October
&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Dec 2018 15:19:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521945#M141655</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-12-17T15:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521986#M141663</link>
      <description>&lt;P&gt;the macro can perform data step functions if you wish:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let cmonth = %sysfunc(  month( "&amp;amp;sysdate"d ) ) ;
%let cmonth_name = %sysfunc( putn( "&amp;amp;sysdate"d , monname12. ) ) ;

%put *** &amp;amp;=cmonth ;
%put *** &amp;amp;=cmonth_name ;

%let pmonth = %sysfunc( month ( %sysfunc( intnx (month , "&amp;amp;sysdate"d , -1 ) ) ) ) ;
%let pmonth_name = %sysfunc( putn( %sysfunc(intnx( month , "&amp;amp;sysdate"d , -1 ) ) , monname12 ) )   ;
%put *** &amp;amp;=pmonth ;
%put *** &amp;amp;=pmonth_name ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Dec 2018 17:52:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/521986#M141663</guid>
      <dc:creator>johnsville</dc:creator>
      <dc:date>2018-12-17T17:52:09Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/522193#M141732</link>
      <description>&lt;PRE&gt;

%let cmonth = %sysfunc(  month( "&amp;amp;sysdate"d ) ,monname8. ) ;

%put &amp;amp;cmonth;


&lt;/PRE&gt;</description>
      <pubDate>Tue, 18 Dec 2018 13:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/522193#M141732</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-12-18T13:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: Extract Month Name from Month Number in a Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/723300#M224423</link>
      <description>&lt;P&gt;FYI - This code returns the wrong value for cmonth_name. Don't use the month() function when using monname. The format needs to be used with a date.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I modified here for the correct solution for the request. Notice the difference in line 3:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('cmonth',month(today()));
call symputx('cmonth_name',put(&lt;STRONG&gt;today()&lt;/STRONG&gt;,monname.));
call symputx('pmonth',month(intnx('month',today(),-1)));
call symputx('cyear',year(today()));
call symputx('pyear',year(intnx('month',today(),-1)));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Mar 2021 22:11:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-Month-Name-from-Month-Number-in-a-Macro/m-p/723300#M224423</guid>
      <dc:creator>malmario</dc:creator>
      <dc:date>2021-03-03T22:11:13Z</dc:date>
    </item>
  </channel>
</rss>

