<?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: Copy range of data from one column to another with different starting columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929457#M365716</link>
    <description>&lt;P&gt;I thought using a simple dataset would be easier but I also didn't want to leave out the background explanation. I see how this is confusing. I will take this into consideration next time, thanks.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 May 2024 18:22:09 GMT</pubDate>
    <dc:creator>rox26</dc:creator>
    <dc:date>2024-05-23T18:22:09Z</dc:date>
    <item>
      <title>Copy range of data from one column to another with different starting columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929410#M365705</link>
      <description>&lt;P&gt;I'm conducting an analysis on SAS 9.4 with health insurance enrollment data and want to understand the enrollment patterns before and after a diagnosis. I have monthly data for several years which I want to select 24 rolling months based around a diagnosis date. I want to copy over the select months into new columns based on the start month range. I have a dummy example below where month1-month7 are the enrollment months 1 through 7 and start_month is the number of the start month and end _month is the number of the end month of the range. I want to copy the data into the "next" variables for a range of 4.&amp;nbsp; Adding "output" into the code doesn't solve the issue. The desired output at the end of this code. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*table creation for 2 examples;&lt;BR /&gt;data try1;&lt;BR /&gt;input month1-month7 start_month end_month;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2 3 4 5 6 7 2 4&lt;BR /&gt;1 2 3 4 5 6 7 4 7&lt;BR /&gt;;&lt;BR /&gt;run; &lt;BR /&gt;proc print data=try1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot1.png" style="width: 606px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96692i2F9C79A358BEF405/image-dimensions/606x87?v=v2" width="606" height="87" role="button" title="Screenshot1.png" alt="Screenshot1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;*This straight copies the data into the "next" variables, not based on start/end range;&lt;BR /&gt;data try2;&lt;BR /&gt;set try1;&lt;BR /&gt;array month month1-month7; /*enrollment months*/&lt;BR /&gt;array next next1-next4; /*want to copy over select months here*/&lt;/P&gt;&lt;P&gt;do i=1 to 4; /*interested in range of 4 months*/&lt;BR /&gt;next{i}=month{i}; /*this straight copies the data*/&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;proc print data=try2;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot2.png" style="width: 698px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96693i71C90DF1C9A1635B/image-dimensions/698x75?v=v2" width="698" height="75" role="button" title="Screenshot2.png" alt="Screenshot2.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;*HERE IS WHERE I AM STUCK;&lt;BR /&gt;*Trying to copy the select months based on the start and end range;&lt;BR /&gt;*This code below only copies the last select month, which is wrong;&lt;BR /&gt;data try3;&lt;BR /&gt;set try1;&lt;BR /&gt;array month month1-month7; /*enrollment months*/&lt;BR /&gt;array next next1-next4; /*want to copy over select months here*/&lt;BR /&gt;do k=start_month to end_month; /*start month and end month for each person*/&lt;BR /&gt;do i=1 to 4; /*interested in range of 4 months*/&lt;BR /&gt;next{i}=month{k}; /*this code is incorrect, it just copies the last month */&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;proc print data=try3;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot3.png" style="width: 712px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96694i0A3E42A42F0D51F8/image-dimensions/712x73?v=v2" width="712" height="73" role="button" title="Screenshot3.png" alt="Screenshot3.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*This is the desired output*/ &lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;input month1-month7 start_month end_month next1-next4;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2 3 4 5 6 7 2 4 2 3 4 .&lt;BR /&gt;1 2 3 4 5 6 7 4 7 4 5 6 7&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc print data=want;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot4.png" style="width: 697px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/96695i62A741E8C4000643/image-dimensions/697x75?v=v2" width="697" height="75" role="button" title="Screenshot4.png" alt="Screenshot4.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 15:30:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929410#M365705</guid>
      <dc:creator>rox26</dc:creator>
      <dc:date>2024-05-23T15:30:24Z</dc:date>
    </item>
    <item>
      <title>Re: Copy range of data from one column to another with different starting columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929425#M365706</link>
      <description>&lt;P&gt;And what if start_month through end_month designate a span more than 4 months?&amp;nbsp; Do you still want only next1-next4?&amp;nbsp; &amp;nbsp;In other words, what is the longest range you want to accommodate for the NEXT variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But if all you want are next1-next4, and start_month through end_month is never more than 4 months, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data try1;
input month1-month7 start_month end_month;
datalines;
1 2 3 4 5 6 7 2 4
1 2 3 4 5 6 7 4 7
run;

data want (drop=n);
  set try1;
  array nxt {4} next1-next4;

  do n=1 to  1 + end_month - start_month;
    nxt{n} = start_month-1+n;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notice there is no array in this code for the MONTH variables.&amp;nbsp; That's because the VALUE of each month exactly equals its array POSITION.&amp;nbsp; &amp;nbsp; But if MONTH1-MONTH7 have other values, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=n);
  set try1;
  array mon {*} month1-month7;
  array nxt {4} next1-next4;

  do n=1 to  1 + end_month - start_month;
    nxt{n} = mon{start_month-1+n};
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 16:29:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929425#M365706</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-05-23T16:29:53Z</dc:date>
    </item>
    <item>
      <title>Re: Copy range of data from one column to another with different starting columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929431#M365707</link>
      <description>&lt;P&gt;I don't see anything resembling a diagnosis date or enough data to do anything resembling 24 months around that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is very likely that the process is much cleaner with actual date values but I'm not sure that I understand how this step even relates to the desription.&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 16:36:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929431#M365707</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-05-23T16:36:43Z</dc:date>
    </item>
    <item>
      <title>Re: Copy range of data from one column to another with different starting columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929452#M365715</link>
      <description>&lt;P&gt;The latter works perfectly with my real data, thank you SO much!&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 18:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929452#M365715</guid>
      <dc:creator>rox26</dc:creator>
      <dc:date>2024-05-23T18:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Copy range of data from one column to another with different starting columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929457#M365716</link>
      <description>&lt;P&gt;I thought using a simple dataset would be easier but I also didn't want to leave out the background explanation. I see how this is confusing. I will take this into consideration next time, thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 May 2024 18:22:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Copy-range-of-data-from-one-column-to-another-with-different/m-p/929457#M365716</guid>
      <dc:creator>rox26</dc:creator>
      <dc:date>2024-05-23T18:22:09Z</dc:date>
    </item>
  </channel>
</rss>

