<?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 output the same number of datasets as the number of macro variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933134#M367029</link>
    <description>&lt;P&gt;If you define a macro you can use %DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to 6 ;
CREATE TABLE NewDS_&amp;amp;&amp;amp;Month_Key&amp;amp;i AS
  SELECT Month_Key, SUM(Var1) as Var1, SUM(Var2) as Var2, SUM(Var3) as Var3
  FROM SourceDS_&amp;amp;&amp;amp;MonthKey&amp;amp;i
  WHERE Var4 = "Adj520"
  GROUP BY 1
;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that it will probably be faster to use PROC SUMMARY than PROC SQL.&amp;nbsp; It is certainly easier to type the code (not so many pesky commas) and extend the list of variables to sum.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=SourceDS_&amp;amp;&amp;amp;MonthKey&amp;amp;i nway;
  WHERE Var4 = "Adj520";
  class Month_Key ;
  var Var1 Var2 Var3 ;
  output out=NewDS_&amp;amp;&amp;amp;Month_Key&amp;amp;i sum=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 Jun 2024 12:43:14 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-06-20T12:43:14Z</dc:date>
    <item>
      <title>How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/932918#M366966</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;My challenge is to find a way of enabling SAS to output the number of datasets determined by the number of month macro variables.&lt;/P&gt;&lt;P&gt;The source datasets are large and therefore created for&lt;STRONG&gt; each separate month&lt;/STRONG&gt;.&lt;/P&gt;&lt;P&gt;However, I need to compare summary results of specific variables across the months.&lt;/P&gt;&lt;P&gt;The code I've written &lt;EM&gt;partially&lt;/EM&gt; automates the data gathering step. It's the second part which I've not yet found an elegant way of enabling SAS to create the relevant number of datasets, hence my question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;First part/section of code declares:&lt;/P&gt;&lt;P&gt;%LET CurrentMonth = '30Jun2024'D&lt;/P&gt;&lt;P&gt;%LET INTNXIncrement = 6;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Two DATA steps follow creating the "MonthKeys" needed because the source dataset names end with the suffix yyyymm, e.g. SourceDS_202405, SourceDS_202404, etc.&lt;/P&gt;&lt;P&gt;These two DATA steps create the relevant MonthKeys via an INTNX statement in which the increment macro is called, in this case 6 for six (&lt;EM&gt;prior&lt;/EM&gt;) months, i.e.:&lt;/P&gt;&lt;P&gt;DATA DS_1;&lt;/P&gt;&lt;P&gt;FORMAT MonthEndDates DATE9.;&lt;/P&gt;&lt;P&gt;DO i=1 TO &amp;amp;INTNXIncrement;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;MonthEndDates = INTNX('MONTH',&amp;amp;CurrentMonth,-i,'E');&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;OUTPUT;&lt;/P&gt;&lt;P&gt;END;&lt;/P&gt;&lt;P&gt;DATA DS_2;&lt;/P&gt;&lt;P&gt;SET DS_1;&lt;/P&gt;&lt;P&gt;IF LENGTH(STRIP(MONTH(MonthEndDates ))) = 1 THEN MonthKeys=YEAR(MonthEndDates )||"0"||STRIP(MONTH(MonthEndDates ));&lt;BR /&gt;ELSE MonthKeys=YEAR(MonthEndDates )||STRIP(MONTH(MonthEndDates ));&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;The DO loop outputs the end dates of the 6 months &lt;EM&gt;preceding&lt;/EM&gt; the current month, the&amp;nbsp;second DATA step simply converts these month-end dates to the yyymm format.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Finally, those MonthKeys are then converted into macro variables via a PROC SQL step:&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT COUNT(*) INTO :NObs&lt;BR /&gt;FROM DS_2;&lt;BR /&gt;SELECT MonthKeys INTO :Month_Key1-:Month_Key%LEFT(&amp;amp;NObs)&lt;BR /&gt;FROM DS_2;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;We now have an array of 6 macro variables in the yyymm format going back 6 months before the current month, i.e.:&lt;/P&gt;&lt;P&gt;Month_Key1, Month_Key2... MonthKey6 or&lt;/P&gt;&lt;P&gt;202405, 202404... 202312.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the last step needed is one capable of outputting only the relevant number of datasets (in this case 6).&lt;/P&gt;&lt;P&gt;It would know to create datasets until the last macro variable is called and processed. Each step or iteration would refer to a different source dataset distinguished by the suffix and those suffixes are already stored in memory as macro variables.&lt;/P&gt;&lt;P&gt;This would avoid writing the code as shown below:&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;CREATE TABLE NewDS_&lt;STRONG&gt;&amp;amp;Month_Key1&lt;/STRONG&gt; AS&lt;/P&gt;&lt;P&gt;SELECT Month_Key, SUM(Var1), SUM(Var2), SUM(Var3)&lt;/P&gt;&lt;P&gt;FROM SourceDS_&lt;STRONG&gt;&amp;amp;MonthKey1&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;GROUP BY 1;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;CREATE TABLE NewDS_&lt;STRONG&gt;&amp;amp;Month_Key6&lt;/STRONG&gt; AS&lt;/P&gt;&lt;P&gt;SELECT Month_Key, SUM(Var1), SUM(Var2), SUM(Var3)&lt;/P&gt;&lt;P&gt;FROM SourceDS_&lt;STRONG&gt;&amp;amp;MonthKey6&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;GROUP BY 1;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;&lt;P&gt;I.e. the total PROC SQL hard-coded steps being dependent on the number of months being analysed, which is no where near ideal.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ultimately, all these grouped/summarised datasets need to be concatenated so that a monthly comparison can be made of each variable.&lt;/P&gt;&lt;P&gt;Hence, this last DATA step also needs refinement, instead of the hard-code approach currently used -&lt;/P&gt;&lt;P&gt;DATA NewDS_Combined;&lt;/P&gt;&lt;P&gt;SET&amp;nbsp;NewDS_&amp;amp;Month_Key1...&amp;nbsp;NewDS_&amp;amp;Month_Key6;&lt;/P&gt;&lt;P&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My guess is either a DO loop or a macro DATA step is required to create the "n" number of datasets and then to concatenate/set them.&lt;/P&gt;&lt;P&gt;I would be very grateful for your advice.&lt;/P&gt;&lt;P&gt;Dan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 01:32:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/932918#M366966</guid>
      <dc:creator>Dansas5</dc:creator>
      <dc:date>2024-06-19T01:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/932928#M366971</link>
      <description>&lt;P&gt;Besides not understanding why you need multiple data sets I don't believe that I understand what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please show the exact list of names you want to create for that given Currentdate and Intnxincrement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note: If you find yourself tempted to write stuff like this again look to a proper FORMAT. SAS even allows creating custom formats for dates if needed&lt;/P&gt;
&lt;P&gt;but&lt;/P&gt;
&lt;PRE&gt;IF LENGTH(STRIP(MONTH(MonthEndDates ))) = 1 THEN MonthKeys=YEAR(MonthEndDates )||"0"||STRIP(MONTH(MonthEndDates ));
ELSE MonthKeys=YEAR(MonthEndDates )||STRIP(MONTH(MonthEndDates ));&lt;/PRE&gt;
&lt;P&gt;one has problems as the automatic conversions for numeric to numeric can be proplematic&lt;/P&gt;
&lt;P&gt;but try&lt;/P&gt;
&lt;PRE&gt;monthkeys = put(monthenddates,yymmn6.);&lt;/PRE&gt;
&lt;P&gt;(not terribly fond of variables indicating plurals as they generally should only have one value at a time)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And this makes the macro variables directly in the first step:&lt;/P&gt;
&lt;PRE&gt;DATA _null_;
  DO i=1 TO &amp;amp;INTNXIncrement;
     call symputx("Month_key"||put(i,best3. -L),put(INTNX('MONTH',&amp;amp;CurrentMonth,-i,'E'),yymmn6.));
  end;
run;&lt;/PRE&gt;
&lt;P&gt;I don't see anything actually related to extracting or creating multiple data sets from one so I suspect it is problematic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Personally I suspect you would be &lt;STRONG&gt;way better off&lt;/STRONG&gt; creating a start and end date of interest, subsetting the large data set into one based on those two dates, sort by the date variable and use:&lt;/P&gt;
&lt;PRE&gt;Proc summary data=smallerdataset;
   by datevalue;
   format datevalue yymmn6.
   var var1 var2 var3 ;
   output out=want (drop=_type_ _freq_) sum= ;
run;&lt;/PRE&gt;
&lt;P&gt;summary and single result set in one step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Look up CALL EXECUTE as a way to create code using values in a data set (or loop).&lt;/P&gt;
&lt;P&gt;Or use a data step to write text for the syntax to a file and then use %include to execute it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2024 05:38:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/932928#M366971</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-19T05:38:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933062#M366999</link>
      <description>&lt;P&gt;Many and sincere thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;for your advice.&lt;/P&gt;&lt;P&gt;Apologies for not making the need clear.&lt;/P&gt;&lt;P&gt;The source datasets I am working with are all &lt;STRONG&gt;monthly tables&lt;/STRONG&gt;, so each dataset relates to data for each month separately.&lt;/P&gt;&lt;P&gt;Therefore, to analyse how a variable changes across/over months it is necessary to combine or concatenate data from the relevant monthly datasets.&lt;/P&gt;&lt;P&gt;In short, I'm not dealing with one large dataset that needs subsetting by the month keys.&lt;/P&gt;&lt;P&gt;These monthly dataset names all follow this convention, i.e. x_y where x = an unchanging string and y being the plural yyyymm reference.&lt;/P&gt;&lt;P&gt;In creating the yyyymm macro variables, I was heading towards the PUT statement you've suggested, which is a far superior approach, so cheers for that.&lt;/P&gt;&lt;P&gt;I obviously need to familiarise myself more with CALL SYMPUTX (have often used CALL SYMPUT previously though).&lt;/P&gt;&lt;P&gt;May I ask what is the purpose of the "&lt;STRONG&gt;-L&lt;/STRONG&gt;" in the first PUT statement in the DATA _null_ step? I.e.,&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;put(i,best3. &lt;STRONG&gt;-L)&lt;/STRONG&gt;&lt;/PRE&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;, your DATA _null_ step involving the CALL SYMPUTX routine achieves in one step what I was trying to achieve in three, which is &lt;FONT color="#0000FF"&gt;very cool.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;First argument creates the macro variable: Month_Key1...Month_Key6. I'm guessing the "&lt;STRONG&gt;-L&lt;/STRONG&gt;" is similar to the low in PROC FORMAT specifying the lowest range of values? I'm curious to know what would happen if we didn't use the "L" specification?&lt;/P&gt;&lt;P&gt;Second argument provides/passes the text for the macro variable to store, which are the month-end dates outputted into yyymm format.&lt;/P&gt;&lt;P&gt;So, I only still need to know a way of instructing SAS to create the "i" number of monthly datasets that ultimately need concatenating.&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 00:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933062#M366999</guid>
      <dc:creator>Dansas5</dc:creator>
      <dc:date>2024-06-20T00:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933069#M367005</link>
      <description>&lt;P&gt;First a simple answer: the -L in put makes sure the result is LEFT justified. Otherwise a put with a numeric value is right justified in the result. Which means you can get leading spaces in the result which remain when concatenated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;IF you have a number of sets to concatenate&lt;/P&gt;
&lt;PRE&gt;data want;
    set  data_202405 data_202404 data_202403 &amp;lt;etc&amp;gt;;
run;&lt;/PRE&gt;
&lt;P&gt;Which with call execute could be written in a data step&lt;/P&gt;
&lt;PRE&gt;data _null_;
   call execute "data want;"
   call execute " set ";

  DO i=1 TO &amp;amp;INTNXIncrement;
     dsn= " sourceDs_"||put(INTNX('MONTH',&amp;amp;CurrentMonth,-i,'E'),yymmn6.));
     call execute (dsn); /* this should be just the name of data set(s) on the Set statement*/
  end;
  call execute (';');/* this ; ends the SET statement*/
  /* if you want to manipulate the variables in the set
  that code would go here
  */
  call execute ('run;';
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 02:35:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933069#M367005</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-20T02:35:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933074#M367007</link>
      <description>&lt;P&gt;Take a step back and explain exactly what SAS you want to end up running when you have a start date of&amp;nbsp;&lt;SPAN&gt;'30Jun2024'D and you want to include 3 months of data.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Do you want to combine the data and then run the analysis?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data for_analysis/ view=for_analysis;
  set biglib.ds_202404 biglib.ds_202405 ;
run;
proc means data=for_analysis noprint;
  var a bc ;
  output out=want sum=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Or do you want to run the analysis and then combine the results?&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=biglib.ds_202404;
  var a bc ;
  output out=summary_202404 sum=;
run;
proc means data=biglib.ds_202405;
  var a bc ;
  output out=summary_202405 sum=;
run;
data want;
  set summary_202404 summary_202405;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Once you know that then you can design some logic to generate that code.&amp;nbsp; Either with your data step that is currently generating the list of months.&amp;nbsp; Or perhaps with a SAS macro instead.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 02:49:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933074#M367007</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-20T02:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933078#M367010</link>
      <description>&lt;P&gt;I thing you are making this way too hard, but let's just go through what you posted in order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First there is no need to run two data steps to calculate two variables.&amp;nbsp; Calculate them both in the same step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DS_2;
  do i=1 to &amp;amp;INTNXIncrement;
    MonthEndDates = intnx('month',&amp;amp;CurrentMonth,-i,'E');
    MonthKeys = put(MonthEndDates,yymmn6.);
    output;
  end;
  format MonthEndDates DATE9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1718852386787.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/97653iFDDB78D8FE37C826/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1718852386787.png" alt="Tom_0-1718852386787.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Now that you have the set of values you can use it to generate the code you want.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;file code temp;
data _null_;
  set ds_2;
  file code;
  put 'proc summary data=SourceDS_' MOnthKeys 'nway;'
    / '  class Month_Key;'
    / '  var var1 var2 var3;'
    / '  output out=NewDS_' MOnthKeys 'sum= ;'
    / 'run;'
    / 'proc append base=NewDS_Combined data=NewDS_' MOnthKeys 'force;'
    / 'run;'
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which you can then easily run using %INCLUDE macro statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%include code / source2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really want to use macro variables instead then there is not really any need for the dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  do i=1 to &amp;amp;INTNXIncrement;
    MonthEndDates = INTNX('MONTH',&amp;amp;CurrentMonth,-i,'E');
    call symputx(cats('month_key',i),put(MonthEndDates,yymmn6.));
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;704  %put &amp;amp;=month_key1 MONTH_KEY&amp;amp;INTNXIncrement=&amp;amp;&amp;amp;&amp;amp;month_key&amp;amp;INTNXIncrement;
MONTH_KEY1=202405 MONTH_KEY6=202312
&lt;/PRE&gt;
&lt;P&gt;Then inside of a macro definition you could use a %DO loop to generate the SAS code instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to &amp;amp;INTNXIncrement;
proc summary data=SourceDS_&amp;amp;&amp;amp;month_key&amp;amp;i nway;
  class Month_Key;
  var var1 var2 var3;
  output out=NewDS_&amp;amp;&amp;amp;month_key&amp;amp;i sum= ;
run;
proc append base=NewDS_Combined data=NewDS_&amp;amp;&amp;amp;month_key&amp;amp;i force;
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could even skip all of the extra macro variables and just have the %DO loop generate the next month_key when it needs it.&amp;nbsp; While you are at it why not generate them in chronological order to begin with?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=&amp;amp;INTNXIncrement %to 1 %by -1;
  %let month_key=%sysfunc(intnx(month,&amp;amp;CurrentMonth,-&amp;amp;i,e),yymmn6.);
proc summary data=SourceDS_&amp;amp;month_key nway;
  class Month_Key;
  var var1 var2 var3;
  output out=NewDS_&amp;amp;month_key sum= ;
run;
proc append base=NewDS_Combined data=NewDS_&amp;amp;month_key force;
run;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 03:27:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933078#M367010</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-20T03:27:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933079#M367011</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Many thanks for your quick reply.&lt;/P&gt;&lt;P&gt;What you've suggested serves for the very last, ultimate, step of concatenating the six, in this case (as INTNXIncrement=6) new datasets ("&lt;FONT color="#FF6600"&gt;NewDS&lt;/FONT&gt;").&lt;/P&gt;&lt;P&gt;We've created the relevant macro variables (Month_Key1... MonthKey6) through that DATA _null_ step you've suggested.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now we need to create 6 new datasets. I did it the long way of hardcoding for each, as shown below.&lt;/P&gt;&lt;P&gt;This is the crucial step we need a solution for and that would manage any number of new monthly datasets.&lt;/P&gt;&lt;P&gt;Remembering that our source datasets are large monthly tables, and we need to query a sub-set number of each of them. That sub-set is determined by the CurrentMonth and INTNXIncrement macro vars.&lt;/P&gt;&lt;P&gt;Also, please accept my apologies for forgetting to write the WHERE clause in the PROC SQL steps I referred to initially in my post. I've now corrected that as shown below.&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, thanks for your reply too, just seen it. To your question "Do you want to combine the data and then run the analysis?', the answer is yes.&lt;/P&gt;&lt;P&gt;That's because each of the six (in this case) datasets will only contain &lt;EM&gt;one&lt;/EM&gt; observation, i.e., the Month_Key value and the summations of the variables (Var1-Var3).&lt;/P&gt;&lt;P&gt;The concatenated dataset will therefore contain six observations, with six Month_Key values and their corresponding summations for the three selected variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Month_Key&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var1&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var2&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var3&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;202405&lt;/TD&gt;&lt;TD&gt;12350&lt;/TD&gt;&lt;TD&gt;32121&lt;/TD&gt;&lt;TD&gt;53210&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;202404&lt;/TD&gt;&lt;TD&gt;12340&lt;/TD&gt;&lt;TD&gt;32101&lt;/TD&gt;&lt;TD&gt;43210&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;202403&lt;/TD&gt;&lt;TD&gt;12303&lt;/TD&gt;&lt;TD&gt;32020&lt;/TD&gt;&lt;TD&gt;32030&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;202402&lt;/TD&gt;&lt;TD&gt;12320&lt;/TD&gt;&lt;TD&gt;32003&lt;/TD&gt;&lt;TD&gt;32020&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;202401&lt;/TD&gt;&lt;TD&gt;12301&lt;/TD&gt;&lt;TD&gt;32140&lt;/TD&gt;&lt;TD&gt;32110&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;202312&lt;/TD&gt;&lt;TD&gt;12321&lt;/TD&gt;&lt;TD&gt;32150&lt;/TD&gt;&lt;TD&gt;32112&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can we perhaps create a macro capable of creating the required number of datasets, in which each iteration the relevant macro variable is passed as a parameter?&lt;/P&gt;&lt;P&gt;E.g. in the first iteration of the macro routine, Month_Key = 202405, and table NewDS_202405 is created by subsetting from SourceDS_202405.&lt;/P&gt;&lt;P&gt;In second iteration, Month_Key = 202404, and table NewDS_202404 is created by subsetting from SourceDS_202404.&lt;/P&gt;&lt;P&gt;Etc. until we reach the sixth macro variable where Month_Key = 202312.&lt;/P&gt;&lt;P&gt;This is what I'm envisioning and I trust the requirements are clearer now. Please advise either way, your help here is hugely appreciated!&lt;/P&gt;&lt;P&gt;The code excerpt below is the undynamic and laborious approach I've temporarily used.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;/P&gt;&lt;P&gt;CREATE TABLE &lt;FONT color="#FF6600"&gt;NewDS&lt;/FONT&gt;_&lt;STRONG&gt;&amp;amp;Month_Key1&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;AS&lt;/P&gt;&lt;P&gt;SELECT Month_Key, SUM(Var1), SUM(Var2), SUM(Var3)&lt;/P&gt;&lt;P&gt;FROM SourceDS_&lt;STRONG&gt;&amp;amp;MonthKey1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;WHERE Var4 = "Adj520";&lt;/P&gt;&lt;P&gt;GROUP BY 1;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;CREATE TABLE &lt;FONT color="#FF6600"&gt;NewDS&lt;/FONT&gt;_&lt;STRONG&gt;&amp;amp;Month_Key6&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;AS&lt;/P&gt;&lt;P&gt;SELECT Month_Key, SUM(Var1), SUM(Var2), SUM(Var3)&lt;/P&gt;&lt;P&gt;FROM SourceDS_&lt;STRONG&gt;&amp;amp;MonthKey6&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;WHERE Var4 = "Adj520";&lt;/P&gt;&lt;P&gt;GROUP BY 1;&lt;/P&gt;&lt;P&gt;QUIT;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 04:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933079#M367011</guid>
      <dc:creator>Dansas5</dc:creator>
      <dc:date>2024-06-20T04:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933134#M367029</link>
      <description>&lt;P&gt;If you define a macro you can use %DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%do i=1 %to 6 ;
CREATE TABLE NewDS_&amp;amp;&amp;amp;Month_Key&amp;amp;i AS
  SELECT Month_Key, SUM(Var1) as Var1, SUM(Var2) as Var2, SUM(Var3) as Var3
  FROM SourceDS_&amp;amp;&amp;amp;MonthKey&amp;amp;i
  WHERE Var4 = "Adj520"
  GROUP BY 1
;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that it will probably be faster to use PROC SUMMARY than PROC SQL.&amp;nbsp; It is certainly easier to type the code (not so many pesky commas) and extend the list of variables to sum.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=SourceDS_&amp;amp;&amp;amp;MonthKey&amp;amp;i nway;
  WHERE Var4 = "Adj520";
  class Month_Key ;
  var Var1 Var2 Var3 ;
  output out=NewDS_&amp;amp;&amp;amp;Month_Key&amp;amp;i sum=;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2024 12:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933134#M367029</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-20T12:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to output the same number of datasets as the number of macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933617#M367168</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;, apologies for my belated reply.&lt;/P&gt;&lt;P&gt;Your suggestion of combining a DO loop within a macro using a PROC SQL step has worked!&lt;/P&gt;&lt;P&gt;I also created another macro appending all the resulting new monthly summary datasets.&lt;/P&gt;&lt;P&gt;But in the end I managed to incorporate that DATA concatenating step within the existing CreateNewDS macro, as shown below.&lt;/P&gt;&lt;P&gt;Hence, the combination of the DATA _null_ step and the macro completely and beautifully solves my much-needed query.&lt;/P&gt;&lt;P&gt;I am indebted to you both&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;for your assistance, you guys are the real stars!&lt;/P&gt;&lt;P&gt;Last, but not least, thank you for reminding me about PROC SUMMARY. I shall need to refresh my memory of that procedure too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;LIBNAME ...&lt;/P&gt;&lt;P&gt;%LET CurrentMonth = '30Jun2024'D&lt;/P&gt;&lt;P&gt;%LET INTNXIncrement = 6;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;DATA _NULL_;&lt;/DIV&gt;&lt;DIV&gt;DO i=1 TO &amp;amp;INTNXIncrement;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;CALL SYMPUTX("Month_Key"||PUT(i,best3. -L),PUT(INTNX('MONTH',&amp;amp;CurrentMonth,-i,'E'),yymmn6.));&lt;/DIV&gt;&lt;DIV&gt;END;&lt;/DIV&gt;&lt;DIV&gt;RUN;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%MACRO CreateNewDS;&lt;/DIV&gt;&lt;DIV&gt;%DO i=1 %TO 6;&lt;/DIV&gt;&lt;DIV&gt;CREATE TABLE NewDS_&amp;amp;&amp;amp;Month_Key&amp;amp;i AS&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; SELECT Month_Key, SUM(Var1) as Var1, SUM(Var2) as Var2, SUM(Var3) as Var3&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; FROM SourceDS_&amp;amp;&amp;amp;MonthKey&amp;amp;i&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; WHERE Var4 = "Adj520"&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp; GROUP BY 1;&lt;/DIV&gt;&lt;DIV&gt;%END;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;DATA CombinedNewDS;&lt;/DIV&gt;&lt;DIV&gt;SET&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%DO i = 1 %TO 6;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; NewDS_&amp;amp;&amp;amp;Month_Key&amp;amp;i&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;%END;;&lt;/DIV&gt;&lt;DIV&gt;RUN;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%MEND;&lt;/DIV&gt;&lt;DIV&gt;%CreateNewDS&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Tue, 25 Jun 2024 00:24:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-output-the-same-number-of-datasets-as-the-number-of-macro/m-p/933617#M367168</guid>
      <dc:creator>Dansas5</dc:creator>
      <dc:date>2024-06-25T00:24:25Z</dc:date>
    </item>
  </channel>
</rss>

