<?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: Dynamically create Fiscal Year and Quarters variable for repeated ids in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-Fiscal-Year-and-Quarters-variable-for/m-p/787171#M251459</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80890"&gt;@Paakay&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to dynamically create Fiscal Year variable and Quarter for each repeated id in a dataset. There are 144,272 records. Each id is repeated 16 times in the dataset. So first four records for id 1 will be assigned Fiscal Year 2018 and assigned Quarter for 1 for first record, Quarter 2 for second record, Quarter three for third record snd Quarter 4 for the fourth record. Records 5 to 8 for id 1 will be assigned Fiscal Year 2019 and 5th record will be assigned Quarter1, 6th record Quarter 2, 7th record Quarter3 and 8th record Quarter4. I want to generate the Fiscal and Quarter variable dynamically for all records. Can someone help me out. The sas code below produces the output I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;Data have;
input id Fiscal_Year Quarter;
datalines;
1 2018 1
1 2018 2
1 2018 3
1 2018 4
1 2019 1
1 2019 2
1 2019 3
1 2019 4
1 2020 1
1 2020 2
1 2020 3
1 2020 4
1 2021 1
1 2021 2
1 2021 3
1 2021 4
;
run


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Use a Cartesian join&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
     create table want as select a.*,b.fiscal_year,b.quarter
     from some_data_set as a,have as b;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 22 Dec 2021 21:35:02 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-12-22T21:35:02Z</dc:date>
    <item>
      <title>Dynamically create Fiscal Year and Quarters variable for repeated ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-Fiscal-Year-and-Quarters-variable-for/m-p/787170#M251458</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to dynamically create Fiscal Year variable and Quarter for each repeated id in a dataset. There are 144,272 records. Each id is repeated 16 times in the dataset. So first four records for id 1 will be assigned Fiscal Year 2018 and assigned Quarter for 1 for first record, Quarter 2 for second record, Quarter three for third record snd Quarter 4 for the fourth record. Records 5 to 8 for id 1 will be assigned Fiscal Year 2019 and 5th record will be assigned Quarter1, 6th record Quarter 2, 7th record Quarter3 and 8th record Quarter4. I want to generate the Fiscal and Quarter variable dynamically for all records. Can someone help me out. The sas code below produces the output I want.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;Data have;
input id Fiscal_Year Quarter;
datalines;
1 2018 1
1 2018 2
1 2018 3
1 2018 4
1 2019 1
1 2019 2
1 2019 3
1 2019 4
1 2020 1
1 2020 2
1 2020 3
1 2020 4
1 2021 1
1 2021 2
1 2021 3
1 2021 4
;
run


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Dec 2021 21:21:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-Fiscal-Year-and-Quarters-variable-for/m-p/787170#M251458</guid>
      <dc:creator>Paakay</dc:creator>
      <dc:date>2021-12-22T21:21:07Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create Fiscal Year and Quarters variable for repeated ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-Fiscal-Year-and-Quarters-variable-for/m-p/787171#M251459</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80890"&gt;@Paakay&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to dynamically create Fiscal Year variable and Quarter for each repeated id in a dataset. There are 144,272 records. Each id is repeated 16 times in the dataset. So first four records for id 1 will be assigned Fiscal Year 2018 and assigned Quarter for 1 for first record, Quarter 2 for second record, Quarter three for third record snd Quarter 4 for the fourth record. Records 5 to 8 for id 1 will be assigned Fiscal Year 2019 and 5th record will be assigned Quarter1, 6th record Quarter 2, 7th record Quarter3 and 8th record Quarter4. I want to generate the Fiscal and Quarter variable dynamically for all records. Can someone help me out. The sas code below produces the output I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;Data have;
input id Fiscal_Year Quarter;
datalines;
1 2018 1
1 2018 2
1 2018 3
1 2018 4
1 2019 1
1 2019 2
1 2019 3
1 2019 4
1 2020 1
1 2020 2
1 2020 3
1 2020 4
1 2021 1
1 2021 2
1 2021 3
1 2021 4
;
run


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Use a Cartesian join&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
     create table want as select a.*,b.fiscal_year,b.quarter
     from some_data_set as a,have as b;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Dec 2021 21:35:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-Fiscal-Year-and-Quarters-variable-for/m-p/787171#M251459</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-12-22T21:35:02Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically create Fiscal Year and Quarters variable for repeated ids</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-Fiscal-Year-and-Quarters-variable-for/m-p/787177#M251461</link>
      <description>&lt;P&gt;1. At the start of each ID set the date to January 1, 2018&lt;/P&gt;
&lt;P&gt;2. Use RETAIN to keep the date variable across rows&lt;/P&gt;
&lt;P&gt;3. If not the first date, increment the date by one quarter using INTNX&lt;/P&gt;
&lt;P&gt;4. Use the YEAR/QTR functions to extract the year/quarter components. Depending on what you're doing, you could consider just keeping the date and using formats.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Tested and works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id;
retain date;

if first.id then date = '01Jan2018'd;
else date = intnx('qtr', date, 1, 's');

year = year(date);
quarter = qtr(date);

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/80890"&gt;@Paakay&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to dynamically create Fiscal Year variable and Quarter for each repeated id in a dataset. There are 144,272 records. Each id is repeated 16 times in the dataset. So first four records for id 1 will be assigned Fiscal Year 2018 and assigned Quarter for 1 for first record, Quarter 2 for second record, Quarter three for third record snd Quarter 4 for the fourth record. Records 5 to 8 for id 1 will be assigned Fiscal Year 2019 and 5th record will be assigned Quarter1, 6th record Quarter 2, 7th record Quarter3 and 8th record Quarter4. I want to generate the Fiscal and Quarter variable dynamically for all records. Can someone help me out. The sas code below produces the output I want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;Data have;
input id Fiscal_Year Quarter;
datalines;
1 2018 1
1 2018 2
1 2018 3
1 2018 4
1 2019 1
1 2019 2
1 2019 3
1 2019 4
1 2020 1
1 2020 2
1 2020 3
1 2020 4
1 2021 1
1 2021 2
1 2021 3
1 2021 4
;
run


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 22:45:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-create-Fiscal-Year-and-Quarters-variable-for/m-p/787177#M251461</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-22T22:45:40Z</dc:date>
    </item>
  </channel>
</rss>

