<?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: By group processing to create multiple rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678387#M204730</link>
    <description>No, Start_mo and End_mo will never be missing. But there are other few&lt;BR /&gt;variables which are missing and I did not mention those variables in my&lt;BR /&gt;post as it is not useful for our calculation.&lt;BR /&gt;</description>
    <pubDate>Fri, 21 Aug 2020 09:27:21 GMT</pubDate>
    <dc:creator>David_Billa</dc:creator>
    <dc:date>2020-08-21T09:27:21Z</dc:date>
    <item>
      <title>By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678362#M204711</link>
      <description>&lt;P&gt;I want to create multiple rows based on time interval between two variables. I've the data as below. We have 'Duration' value in the source data which difference between 'Start_Mo' and 'End_Mo'. Value of 'End_Mo' will always be greater than 'Start_Mo'.&lt;/P&gt;
&lt;P&gt;Source data:&lt;/P&gt;
&lt;PRE&gt;data have;
    input Branch $ Segment $
        Start_Mo :anydtdte7. End_Mo :anydtdte7. Duration Id Cmpny $;
    format  start_mo end_mo yymmd7.;
    datalines;
71831234 7183_AS4     2017-03    2017-09          3     1234   Capgemini
71831248 7183_AS4     2016-12    2017-09          4     78901  TCS
71831234 7183_AS5     2017-03    2017-12          4     1234   Capgemini
71831234 7183_AS5     2017-06    2017-03          5     78901  TCS
;
run;&lt;/PRE&gt;
&lt;P&gt;Now I want to create as many as rows based on the value from the variable 'Duration' and I've to create one more variable called 'Cal_Month' which should have values in quarters between time interval of the variables 'Start_Mo' and 'End_Mo'&lt;BR /&gt;with group by 'Segment' and 'Branch'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Desired Result is,&lt;/P&gt;
&lt;PRE&gt;Branch	Segment	Start_Mo End_Mo	   Duration	Cmpny	       cal_month
71831234 7183_AS4 2017-03 2017-09	3	Capgemin	 2017-03
71831234 7183_AS4 2017-03 2017-09	3	Capgemin	 2017-06
71831234 7183_AS4 2017-03 2017-09	3	Capgemin	 2017-09
71831248 7183_AS4 2016-12 2017-09	4	TCS	         2016-12
71831248 7183_AS4 2016-12 2017-09	4	TCS	         2017-03
71831248 7183_AS4 2016-12 2017-09	4	TCS              2017-06
71831248 7183_AS4 2016-12 2017-09	4	TCS	         2017-09
71831234 7183_AS5 2017-03 2017-12	4	Capgemin	 2017-03
71831234 7183_AS5 2017-03 2017-12	4	Capgemin	 2017-06
71831234 7183_AS5 2017-03 2017-12	4	Capgemin	 2017-09
71831234 7183_AS5 2017-03 2017-12	4	Capgemin	 2017-12
71831234 7183_AS5 2017-03 2018-03	5	TCS	         2017-03
71831234 7183_AS5 2017-03 2018-03	5	TCS	         2017-06
71831234 7183_AS5 2017-03 2018-03	5	TCS	         2017-09
71831234 7183_AS5 2017-03 2018-03	5	TCS	         2017-12
71831234 7183_AS5 2017-03 2018-03	5	TCS	         2018-03
&lt;/PRE&gt;
&lt;P&gt;For example, values of 'Cal_Month' should be in 2017-03, 2017-06 and 2017-09 if 'Duration' value is 3 for each&amp;nbsp; 'Segment'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In real life I've data with 'Duration' from 1 to 9 and 'Segment' is also have multiple values. Just to get the idea to tackle this Scenario, I just shared the sample data here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried with the code as below, but it's not producing the desired results.&lt;/P&gt;
&lt;PRE&gt;data want;
    set have;
    by Segment;
    cal_month=start_mo;

    do until (cal_month &amp;gt; end_mo);
        output;
        cal_month=intnx('month',cal_month,duration);
    end;

    format cal_month yymmd7.;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 10:22:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678362#M204711</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T10:22:17Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678365#M204714</link>
      <description>&lt;P&gt;Your logic is almost there. Try this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   cal_month=start_mo;
   do while (cal_month &amp;lt;= End_Mo);
      output;
      cal_month = intnx('qtr', cal_month, 1, 's');
   end;
   format cal_month yymmd7.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result (Edited to comply with correct sample data)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Branch   Segment  Start_Mo End_Mo  Duration Id    Cmpny    cal_month 
71831234 7183_AS4 2017-03  2017-09 3        1234  Capgemin 2017-03 
71831234 7183_AS4 2017-03  2017-09 3        1234  Capgemin 2017-06 
71831234 7183_AS4 2017-03  2017-09 3        1234  Capgemin 2017-09 
71831234 7183_AS4 2017-03  2017-12 4        78901 TCS      2017-03 
71831234 7183_AS4 2017-03  2017-12 4        78901 TCS      2017-06 
71831234 7183_AS4 2017-03  2017-12 4        78901 TCS      2017-09 
71831234 7183_AS4 2017-03  2017-12 4        78901 TCS      2017-12 
71831234 7183_AS5 2017-03  2017-12 4        1234  Capgemin 2017-03 
71831234 7183_AS5 2017-03  2017-12 4        1234  Capgemin 2017-06 
71831234 7183_AS5 2017-03  2017-12 4        1234  Capgemin 2017-09 
71831234 7183_AS5 2017-03  2017-12 4        1234  Capgemin 2017-12 
71831234 7183_AS5 2017-03  2018-03 5        78901 TCS      2017-03 
71831234 7183_AS5 2017-06  2018-03 5        78901 TCS      2017-06 
71831234 7183_AS5 2017-06  2018-03 5        78901 TCS      2017-09 
71831234 7183_AS5 2017-06  2018-03 5        78901 TCS      2017-12 
71831234 7183_AS5 2017-06  2018-03 5        78901 TCS      2018-03 &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 08:51:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678365#M204714</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-21T08:51:14Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678368#M204715</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;thank you. But it seems do while is taking more time to process with 10000+ records. Is there a chance to do it with other ways?&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 08:37:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678368#M204715</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T08:37:28Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678369#M204716</link>
      <description>&lt;P&gt;More time than what? You can do it with Do Until logic as you tried first, but I doubt that it'll be faster&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   cal_month=start_mo;
   do until (cal_month &amp;gt; End_Mo);
      output;
      cal_month = intnx('qtr', cal_month, 1, 's');
   end;
   format cal_month yymmd7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 08:44:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678369#M204716</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-21T08:44:38Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678370#M204717</link>
      <description>&lt;P&gt;Your code don't seem to work to produce the desired results. I should get 16 records in the desired Output but I could see only 11 after executing the code which you gave. I feel we still need to tweak the logic.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 08:45:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678370#M204717</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T08:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678372#M204718</link>
      <description>&lt;P&gt;The error is in the given sample data in the dates in the last row. This is the correct example data given your desired result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both of my posted code snippets produce the correct result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input Branch $ Segment $
        Start_Mo :anydtdte7. End_Mo :anydtdte7. Duration Id Cmpny $;
    format  start_mo end_mo yymmd7.;
    datalines;
71831234 7183_AS4     2017-03    2017-09          3     1234   Capgemini
71831234 7183_AS4     2017-03    2017-12          4     78901  TCS
71831234 7183_AS5     2017-03    2017-12          4     1234   Capgemini
71831234 7183_AS5     2017-03    2018-03          5     78901  TCS
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 08:50:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678372#M204718</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-21T08:50:00Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678379#M204722</link>
      <description>&lt;P&gt;Yes, your code is working with the example data. When I apply your code in the real life data with 40 variables 10500 observations it keep on running. That's the reason, I asked you to help me with alternative methods. If possible, I would like to know how to achive the results with 'do until'&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 08:59:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678379#M204722</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T08:59:39Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678382#M204725</link>
      <description>&lt;P&gt;This should not take forever. I suspect that this is a data problem. Do you have missing values for&amp;nbsp;Start_Mo?&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:10:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678382#M204725</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-21T09:10:06Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678383#M204726</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292396"&gt;@David_Billa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;thank you. But it seems do while is taking more time to process with 10000+ records.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Only if you work with a mechanical computer from WW2. Anything that ever ran SAS does this in seconds, and nowadays in fractions of a second:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    input Branch $ Segment $
        Start_Mo :anydtdte7. End_Mo :anydtdte7. Duration Id Cmpny $;
    format  start_mo end_mo yymmd7.;
    do i = 1 to 10000;
      output;
    end;
    drop i;
    datalines;
71831234 7183_AS4     2017-03    2017-09          3     1234   Capgemini
71831234 7183_AS4     2017-03    2017-12          4     78901  TCS
71831234 7183_AS5     2017-03    2017-12          4     1234   Capgemini
71831234 7183_AS5     2017-06    2018-03          5     78901  TCS
;

data want;
   set have;
   cal_month=start_mo;
   do while (cal_month &amp;lt;= End_Mo);
      output;
      cal_month = intnx('qtr', cal_month, 1, 's');
   end;
   format cal_month yymmd7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log excerpt:&lt;/P&gt;
&lt;PRE&gt; 88         data want;
 89            set have;
 90            cal_month=start_mo;
 91            do while (cal_month &amp;lt;= End_Mo);
 92               output;
 93               cal_month = intnx('qtr', cal_month, 1, 's');
 94            end;
 95            format cal_month yymmd7.;
 96         run;
 
 NOTE: There were 40000 observations read from the data set WORK.HAVE.
 NOTE: The data set WORK.WANT has 150000 observations and 8 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.16 seconds
       cpu time            0.15 seconds
&lt;/PRE&gt;
&lt;P&gt;But since you have a duration variable already, do it in an iterative do loop:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
format cal_month yymmd7.;
do period = 1 to duration;
  cal_month = intnx('quarter',start_mo,period-1,'b');
  output;
end;
drop period;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;whic is even faster:&lt;/P&gt;
&lt;PRE&gt; 73         data want;
 74         set have;
 75         format cal_month yymmd7.;
 76         do period = 1 to duration;
 77           cal_month = intnx('quarter',start_mo,period-1,'b');
 78           output;
 79         end;
 80         drop period;
 81         run;
 
 NOTE: There were 40000 observations read from the data set WORK.HAVE.
 NOTE: The data set WORK.WANT has 160000 observations and 8 variables.
 NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
       real time           0.09 seconds
       cpu time            0.08 seconds
&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:10:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678383#M204726</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-08-21T09:10:15Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678387#M204730</link>
      <description>No, Start_mo and End_mo will never be missing. But there are other few&lt;BR /&gt;variables which are missing and I did not mention those variables in my&lt;BR /&gt;post as it is not useful for our calculation.&lt;BR /&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678387#M204730</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T09:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678394#M204733</link>
      <description>I believe you have assumed that the value of 'Start_Mo' will always be same for all records. But it's not the case in real life. Therefore in my last row of the sample data I gave other date value in 'Start_mo' which is different from other previous rows.&lt;BR /&gt;&lt;BR /&gt;How can we tweak your ' do while' now?&lt;BR /&gt;</description>
      <pubDate>Fri, 21 Aug 2020 09:57:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678394#M204733</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T09:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678400#M204735</link>
      <description>&lt;P&gt;Can you update your sample data to be representable of your actual data?&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 10:04:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678400#M204735</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-08-21T10:04:13Z</dc:date>
    </item>
    <item>
      <title>Re: By group processing to create multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678404#M204738</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;&amp;nbsp;I have updated the sample data and also the desired result in my initial post. Please note that have to create as many as rows based on two variables 'Segment' and 'Branch' and this is also I have updated in my post. If you see the sample data now, I have updated the value of 'Branch' for one of the record in 'have' data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for your effort in helping me to identify the logic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 10:25:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/By-group-processing-to-create-multiple-rows/m-p/678404#M204738</guid>
      <dc:creator>David_Billa</dc:creator>
      <dc:date>2020-08-21T10:25:38Z</dc:date>
    </item>
  </channel>
</rss>

