<?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: Changing Table Name Autominations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893152#M352830</link>
    <description>&lt;P&gt;I do not have to write the code in sql language. The reason I put the variable year is because there is a table specific to each year. table_2023 only contains data for 2023. I want to use data for the year and the months I want (3,6 and 12). Instead of writing this manually, I want to write a macro like in the example you gave. But I'm not good at macro writing. This example was written for September 2023. In the future (April 2024 for example), when I want data from 3 months ago, I want to automatically assign table_2024 to 6 months and 12 months ago when I want table_2022.&lt;/P&gt;</description>
    <pubDate>Thu, 07 Sep 2023 12:49:57 GMT</pubDate>
    <dc:creator>Burakgns</dc:creator>
    <dc:date>2023-09-07T12:49:57Z</dc:date>
    <item>
      <title>Changing Table Name Autominations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893136#M352824</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table table_2 as&lt;/P&gt;&lt;P&gt;select * from table_2022&amp;nbsp; (I want the table name to change automatically compared to 3.6 and 12 months ago.)&lt;/P&gt;&lt;P&gt;where year = 2022 and month = 08; (I want these fields to come automatically.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;create table table_1 as&amp;nbsp;&lt;/P&gt;&lt;P&gt;select * from table_2023 (I want the table name to change automatically compared to 3.6 and 12 months ago.)&lt;/P&gt;&lt;P&gt;where year = 2023 and month in (05,03)&amp;nbsp; (I want these fields to come automatically.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can give an example like that. I dont want to write year and month manuel. Would we try to write it like symput?&amp;nbsp;As each month changes, I want the month variables in the where constraint to change from manual to automation. Can we write this in one set instead of writing two different sets of code?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2023 11:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893136#M352824</guid>
      <dc:creator>Burakgns</dc:creator>
      <dc:date>2023-09-07T11:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Table Name Autominations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893143#M352826</link>
      <description>&lt;P&gt;In your two examples there are three places where the code is changing.&lt;/P&gt;
&lt;P&gt;So it seems you might benefit from a macro with three input parameters.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro subset(year,months,out);
proc sql;
create table &amp;amp;out as
select * from table_&amp;amp;year 
where year = &amp;amp;year
   and month in (&amp;amp;months)
; 
%mend;
%subset(2022,08,table_1)
%subset(2023,05 03,table_2)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note don't bother using comma as the delimiter in the list of months, it will just make it harder to pass a value to the macro, as the IN operator in SAS allows you use either commas or spaces as the delimiter in the list of values.&lt;/P&gt;
&lt;P&gt;Not sure why you have leading zeros on numbers, but those will not matter.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure why you have to select by the value of YEAR if the dataset name already has the year value in its name.&amp;nbsp; Is it possible that the dataset names TABLE_2023 has some observations where the value of YEAR is not 2023?&amp;nbsp; Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to &lt;STRONG&gt;automate&lt;/STRONG&gt; you need to spell out the rules for how to translate from the concept of "3 months ago" to one of those two SQL codes.&amp;nbsp; What is the rule for generating the values of YEAR?&amp;nbsp; What is the rule for generating the value of MONTHS you want to include?&amp;nbsp; What is the rule for generating the name of the output dataset?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How to you want to encode the concept of "3 months ago" and "6 months ago"?&amp;nbsp; Do you just want to use the digit strings of 3 6 and 12 ?&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2023 11:56:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893143#M352826</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-07T11:56:56Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Table Name Autominations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893152#M352830</link>
      <description>&lt;P&gt;I do not have to write the code in sql language. The reason I put the variable year is because there is a table specific to each year. table_2023 only contains data for 2023. I want to use data for the year and the months I want (3,6 and 12). Instead of writing this manually, I want to write a macro like in the example you gave. But I'm not good at macro writing. This example was written for September 2023. In the future (April 2024 for example), when I want data from 3 months ago, I want to automatically assign table_2024 to 6 months and 12 months ago when I want table_2022.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Sep 2023 12:49:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893152#M352830</guid>
      <dc:creator>Burakgns</dc:creator>
      <dc:date>2023-09-07T12:49:57Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Table Name Autominations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893177#M352836</link>
      <description>&lt;P&gt;So if have as input a number representing a number of months you can use that with INTNX() to find a date that is that number of months ago.&lt;/P&gt;
&lt;P&gt;Once you have a date you can calculate the year of that date and the month of that date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I do not understand what you mean by testing for more than one month value.&amp;nbsp; &amp;nbsp; Do you mean you just want the data for the three individual months that are 3, 6 and 12 months ago?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You keep not explaining what your data actually looks like.&amp;nbsp; Do you have a DATE variable?&amp;nbsp; Or do you have YEAR and MONTH variables?&amp;nbsp; Or do you have something else?&amp;nbsp; Perhaps some character variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since you implied you have YEAR and MONTH variables you could do:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test;
%local today year date month fyear lyear;
%let today=%sysfunc(today());
%let fyear=%sysfunc(intnx(month,&amp;amp;today,-12),year.);
%let lyear=%sysfunc(intnx(month,&amp;amp;today,-3),year.);
data want ;
  set mylib.table_&amp;amp;fyear - mylib.table_&amp;amp;lyear ;
  where 
%let date=%sysfunc(intnx(month,&amp;amp;today,-12));
      (year = %sysfunc(year(&amp;amp;date)) and month=%sysfunc(month(&amp;amp;date)) )
%let date=%sysfunc(intnx(month,&amp;amp;today,-6));
   or (year = %sysfunc(year(&amp;amp;date)) and month=%sysfunc(month(&amp;amp;date)) )
%let date=%sysfunc(intnx(month,&amp;amp;today,-3));
   or (year = %sysfunc(year(&amp;amp;date)) and month=%sysfunc(month(&amp;amp;date)) )
   ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Sep 2023 15:49:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893177#M352836</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-09-07T15:49:52Z</dc:date>
    </item>
    <item>
      <title>Re: Changing Table Name Autominations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893235#M352855</link>
      <description>&lt;P&gt;This isn't necessarily shorter, but is automated. Change the month at the top - which could also be derived from a system date if this is run every month (e.g. today())&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let reportMonth = 202304;

data _null_;
start_date = input("&amp;amp;reportMonth", yymmn6.);

do i=3, 6, 12;
    date = intnx('month', start_date, -1*i, 'b');
    dset_text = catt("table_", year(date), ' (where=(month=', month(date), '))');
    call symputx(catt('source', put(i, 8. -l)), dset_text);
end;

run;


data want;
set &amp;amp;source3. &amp;amp;source6. &amp;amp;source12.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 07 Sep 2023 21:19:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-Table-Name-Autominations/m-p/893235#M352855</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-09-07T21:19:19Z</dc:date>
    </item>
  </channel>
</rss>

