<?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 Generate dynamic picklist of months and years in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Generate-dynamic-picklist-of-months-and-years/m-p/982902#M379410</link>
    <description>&lt;P&gt;I have historical monthly data that is organized under folder names that start with a year and a month.&amp;nbsp; I have a short program that creates a pick list consisting of a year and a month.&amp;nbsp; This data will be used later in a macro to select the datafile within each of the respective unique folder names.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let rmonth = 02;
%let ryear = 2025;
* Assure that RMONTH is zero-filled (eg, 03 not 3) ;&lt;BR /&gt;%let rmonth = %sysfunc(putn(&amp;amp;rmonth,z2.) ) ;&lt;BR /&gt;
* Create list of months for which data are needed. ;
data months ;
Year = &amp;amp;ryear     ; Month = &amp;amp;rmonth ; output ;
year = &amp;amp;ryear - 1 ; month = &amp;amp;rmonth ; output ;
year = &amp;amp;ryear - 1 ; month =      12 ; output ;
year = &amp;amp;ryear - 2 ; month =      12 ; output ;
year = &amp;amp;ryear - 3 ; month =      12 ; output ;
year = &amp;amp;ryear - 4 ; month =      12 ; output ;
year = &amp;amp;ryear - 5 ; month =      12 ; output ;
year = &amp;amp;ryear - 6 ; month =      12 ; output ;
run ;

proc sql ;
create table distinct_months as
 select distinct year , month
  from months
 order by year , month ;
quit ; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works just fine where I am selecting the month for a prior year, and the month of December for 6 prior years.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I want to create a new report, one that selects monthly data from each of the prior 12 months sequentially.&amp;nbsp; &amp;nbsp;The following revision works fine, but only if the month is December.&amp;nbsp; In the other months, to get the prior 12, one needs to -1 (i.e., subtract one from the year) for one or more of the lines and then add 1, 2, 3, etc... to the month, as appropriate, to generate the correct picklist.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data months ;
Year = &amp;amp;ryear  ; month = &amp;amp;rmonth ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -1 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -2 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -3 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -4 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -5 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -6 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -7 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -8 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -9 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -10 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -11 ; output ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I suppose that I could hardcode 12 iterations, one for each starting month and then writing code that tells SAS to use the appropriate iteration based on the starting month.&amp;nbsp; I am hoping there is a more sophisticated way to make this dynamic and avoid 12 sets of instructions.&amp;nbsp; Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 03 Feb 2026 04:21:58 GMT</pubDate>
    <dc:creator>texasmfp</dc:creator>
    <dc:date>2026-02-03T04:21:58Z</dc:date>
    <item>
      <title>Generate dynamic picklist of months and years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-dynamic-picklist-of-months-and-years/m-p/982902#M379410</link>
      <description>&lt;P&gt;I have historical monthly data that is organized under folder names that start with a year and a month.&amp;nbsp; I have a short program that creates a pick list consisting of a year and a month.&amp;nbsp; This data will be used later in a macro to select the datafile within each of the respective unique folder names.&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let rmonth = 02;
%let ryear = 2025;
* Assure that RMONTH is zero-filled (eg, 03 not 3) ;&lt;BR /&gt;%let rmonth = %sysfunc(putn(&amp;amp;rmonth,z2.) ) ;&lt;BR /&gt;
* Create list of months for which data are needed. ;
data months ;
Year = &amp;amp;ryear     ; Month = &amp;amp;rmonth ; output ;
year = &amp;amp;ryear - 1 ; month = &amp;amp;rmonth ; output ;
year = &amp;amp;ryear - 1 ; month =      12 ; output ;
year = &amp;amp;ryear - 2 ; month =      12 ; output ;
year = &amp;amp;ryear - 3 ; month =      12 ; output ;
year = &amp;amp;ryear - 4 ; month =      12 ; output ;
year = &amp;amp;ryear - 5 ; month =      12 ; output ;
year = &amp;amp;ryear - 6 ; month =      12 ; output ;
run ;

proc sql ;
create table distinct_months as
 select distinct year , month
  from months
 order by year , month ;
quit ; 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works just fine where I am selecting the month for a prior year, and the month of December for 6 prior years.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, I want to create a new report, one that selects monthly data from each of the prior 12 months sequentially.&amp;nbsp; &amp;nbsp;The following revision works fine, but only if the month is December.&amp;nbsp; In the other months, to get the prior 12, one needs to -1 (i.e., subtract one from the year) for one or more of the lines and then add 1, 2, 3, etc... to the month, as appropriate, to generate the correct picklist.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data months ;
Year = &amp;amp;ryear  ; month = &amp;amp;rmonth ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -1 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -2 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -3 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -4 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -5 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -6 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -7 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -8 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -9 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -10 ; output ;
year = &amp;amp;ryear  ; month = &amp;amp;rmonth -11 ; output ;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I suppose that I could hardcode 12 iterations, one for each starting month and then writing code that tells SAS to use the appropriate iteration based on the starting month.&amp;nbsp; I am hoping there is a more sophisticated way to make this dynamic and avoid 12 sets of instructions.&amp;nbsp; Thanks&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Feb 2026 04:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-dynamic-picklist-of-months-and-years/m-p/982902#M379410</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2026-02-03T04:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: Generate dynamic picklist of months and years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-dynamic-picklist-of-months-and-years/m-p/982906#M379411</link>
      <description>&lt;P&gt;Do you want to pick up the previous 12 months before a date ?&lt;/P&gt;
&lt;PRE&gt;%let rmonth = 02;
%let ryear = 2025;

data months ;
date=input("&amp;amp;ryear.&amp;amp;rmonth.",yymmn6.);
format date yymmn6.;
do n=0 to -11 by -1;
  year=year(intnx('month',date,n));
  month=month(intnx('month',date,n));
  output;
end;
run;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1770103401494.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/112961iF26E5E3CCE7FF257/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1770103401494.png" alt="Ksharp_0-1770103401494.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 03 Feb 2026 07:23:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-dynamic-picklist-of-months-and-years/m-p/982906#M379411</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-02-03T07:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: Generate dynamic picklist of months and years</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Generate-dynamic-picklist-of-months-and-years/m-p/982909#M379413</link>
      <description>&lt;P&gt;Beautiful concept in its simplicity!&amp;nbsp; Bring them together, generate, separate!&amp;nbsp; Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 03 Feb 2026 08:09:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Generate-dynamic-picklist-of-months-and-years/m-p/982909#M379413</guid>
      <dc:creator>texasmfp</dc:creator>
      <dc:date>2026-02-03T08:09:15Z</dc:date>
    </item>
  </channel>
</rss>

