<?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 last three months columns based on financial year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/702982#M215350</link>
    <description>&lt;P&gt;Having data in the name of variable is almost always a bad idea and leads to unnecessary complex code. To solve your problem start by transposing the data, so that you have a variable with the year/month information. In a subsequent step use that variable to create a sas-date and change the value of your macro-variable to&lt;/P&gt;
&lt;P&gt;%let monyear = '01Apr2020'd;&lt;/P&gt;
&lt;P&gt;so that it contains a sas-date, too. Last step: Use a where statement to select the information of interest, like &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data april;
  set transposed_fixed_have;
  where intnx('month', &amp;amp;monyear. ,  -3) &amp;lt;= dateVar &amp;lt;= &amp;amp;monyear.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 02 Dec 2020 07:00:31 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2020-12-02T07:00:31Z</dc:date>
    <item>
      <title>Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/702976#M215347</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I need to extract last three months columns based on financial year.&lt;/P&gt;
&lt;P&gt;My dataset contains columns like&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;201910&amp;nbsp; &amp;nbsp;201911&amp;nbsp; &amp;nbsp;201912&amp;nbsp; &amp;nbsp;202001&amp;nbsp; &amp;nbsp;202004&amp;nbsp; &amp;nbsp;202005&amp;nbsp; 202006&amp;nbsp; &amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Now suppose if someone will pass a parameter as below&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let monyear=202004&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;then column should get filtered as &lt;STRONG&gt;202001&amp;nbsp; &amp;nbsp;202003&amp;nbsp; and 202004&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;But suppose someone will pass a parameter as below&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;%let monyear=202001&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;then my column should get filtered as &lt;STRONG&gt;201910&amp;nbsp; &amp;nbsp;201912 and 202001&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Kindly help me to solve this issue&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Note: First four digit contains year and last two digits contains month.&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Thanks,&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#000000"&gt;Harsh&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 06:29:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/702976#M215347</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-02T06:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/702982#M215350</link>
      <description>&lt;P&gt;Having data in the name of variable is almost always a bad idea and leads to unnecessary complex code. To solve your problem start by transposing the data, so that you have a variable with the year/month information. In a subsequent step use that variable to create a sas-date and change the value of your macro-variable to&lt;/P&gt;
&lt;P&gt;%let monyear = '01Apr2020'd;&lt;/P&gt;
&lt;P&gt;so that it contains a sas-date, too. Last step: Use a where statement to select the information of interest, like &lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data april;
  set transposed_fixed_have;
  where intnx('month', &amp;amp;monyear. ,  -3) &amp;lt;= dateVar &amp;lt;= &amp;amp;monyear.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Dec 2020 07:00:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/702982#M215350</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-02T07:00:31Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/702985#M215352</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;But my column format is 202004&lt;/P&gt;
&lt;P&gt;and i want to create 3 macro variables&lt;/P&gt;
&lt;P&gt;IF %let year=202004 then %let year1=202001 (year-3)&amp;nbsp; and %let year2=202003 (year-1)&lt;/P&gt;
&lt;P&gt;But if %let year=202001 then %let year1=201910&amp;nbsp; and %let year2=201912&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 07:06:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/702985#M215352</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-02T07:06:54Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703048#M215396</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259069"&gt;@harshpatel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;But my column format is 202004&lt;/P&gt;
&lt;P&gt;and i want to create 3 macro variables&lt;/P&gt;
&lt;P&gt;IF %let year=202004 then %let year1=202001 (year-3)&amp;nbsp; and %let year2=202003 (year-1)&lt;/P&gt;
&lt;P&gt;But if %let year=202001 then %let year1=201910&amp;nbsp; and %let year2=201912&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;2020204 is not a valid name for a sas variable, even if the option validvarname=any is active, it is strongly recommended to use "old-school" variable names. Using "partial" dates makes things worse, and as i already said, unnecessary complicated. With a proper date (as value) you can use the function intnx to shift the value without having to take care to maintain a valid date manually.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 10:15:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703048#M215396</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-02T10:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703074#M215405</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259069"&gt;@harshpatel&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But my column format is 202004&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It simply doesn't matter that your column format is 202004. Transpose the data, and your coding becomes simple. SAS has already done the behind the scenes work to make this simple, if only you will go that way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Leave the data as it is with calendar dates as variable names, and you need macro variables and other complications and your coding becomes difficult.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 11:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703074#M215405</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-12-02T11:55:37Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703078#M215407</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let monyear=202001 ;

%let temp=%sysfunc(inputn( &amp;amp;monyear. , yymmn6.)) ;
%let m1=%sysfunc(intnx(month, &amp;amp;temp , -1 ),yymmn6.);
%let m2=%sysfunc(intnx(month, &amp;amp;temp , -2 ),yymmn6.);


%put &amp;amp;m2 &amp;amp;m1 &amp;amp;monyear ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Dec 2020 12:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703078#M215407</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-12-02T12:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703090#M215408</link>
      <description>&lt;P&gt;Please believe us when we give you advice, born from decades of experience in the field. A dataset structure like yours is&amp;nbsp;&lt;STRONG&gt;USELESS&lt;/STRONG&gt;. TRANSPOSE before you do anything else. Really. I mean it. Ask your Grandpa if you don't believe me.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 13:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703090#M215408</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-02T13:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703094#M215409</link>
      <description>Hi,&lt;BR /&gt;I have not written any contradictory statement, I have just asked for help and when I got solution I have just thanks to those who has given me solution,&lt;BR /&gt;So don't say any contradictory statement&lt;BR /&gt;Be professional &lt;BR /&gt;</description>
      <pubDate>Wed, 02 Dec 2020 13:17:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703094#M215409</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-02T13:17:00Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703096#M215410</link>
      <description>I have already accepted the solution&lt;BR /&gt;And next time don't say that ask your grandpa,&lt;BR /&gt;I know you are a born decade expert and a experienced guy but don't be rude and arrogant.&lt;BR /&gt;If you can't do help then simply tell that this is not possible</description>
      <pubDate>Wed, 02 Dec 2020 13:26:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703096#M215410</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-02T13:26:33Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703097#M215411</link>
      <description>&lt;P&gt;I AM being professional, and I AM trying to help you out of 20+ years of SAS experience and close to 4 decades as a programmer.&lt;/P&gt;
&lt;P&gt;So suppose someone gave you a spreadsheet (most of such useless data structure comes from there) like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data bad;
input ID $ '201910'n '201911'n '201912'n '202001'n '202002'n '202003'n '202004'n '202005'n '202006'n;
datalines;
A 1 2 3 4 5 6 7 8 9
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The first thing you do is make this unintelligent and unhelpful data into intelligent data (see Maxim 33):&lt;/P&gt;
&lt;P&gt;First, transpose, so that data goes from structure to content:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose
  data=bad
  out=long
;
by ID;
var '201910'n--'202006'n;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Next, make &lt;EM&gt;date values&lt;/EM&gt; out of the strings:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data good;
set long;
period = input(_name_,yymmn6.);
format period yymmd7.;
rename col1=value;
drop _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, your initial issue turns into extremely simple code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let monyear=202004;
/* make this value into a SAS date value: */
%let period=%sysfunc(inputn(&amp;amp;monyear,yymmn6.));

data want;
set good;
where intnx('month',&amp;amp;period.,-2,'b') le period le &amp;amp;period.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can find a gazillion of posts here on the communities where the merits of a long dataset structure are shown and proven. Please heed our advice.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 13:27:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703097#M215411</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-02T13:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703103#M215412</link>
      <description>Thanks for your help,&lt;BR /&gt;I will apply this logic.&lt;BR /&gt;It will definitely work</description>
      <pubDate>Wed, 02 Dec 2020 13:46:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703103#M215412</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-02T13:46:26Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703105#M215413</link>
      <description>&lt;P&gt;And PS&lt;/P&gt;
&lt;P&gt;That "Grandpa" reference was a meant to be a joke to lighten up the discussion.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Dec 2020 13:49:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703105#M215413</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-12-02T13:49:26Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703108#M215414</link>
      <description>Yup it's ok,&lt;BR /&gt;I can understand&lt;BR /&gt;Thanks for your help</description>
      <pubDate>Wed, 02 Dec 2020 14:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/703108#M215414</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-02T14:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: Extract last three months columns based on financial year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/704030#M215782</link>
      <description>&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Its works for me&lt;/P&gt;</description>
      <pubDate>Mon, 07 Dec 2020 03:39:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extract-last-three-months-columns-based-on-financial-year/m-p/704030#M215782</guid>
      <dc:creator>harshpatel</dc:creator>
      <dc:date>2020-12-07T03:39:20Z</dc:date>
    </item>
  </channel>
</rss>

