<?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 Concatenate if same month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-if-same-month/m-p/345212#M79385</link>
    <description>I have a dataset with id, release_dare, and a a variable called datenum&lt;BR /&gt;&lt;BR /&gt;Id $4&lt;BR /&gt;release_date mmddyy.&lt;BR /&gt;Datenum $4&lt;BR /&gt;&lt;BR /&gt;There are several rows per id since each can have many release dates.  &lt;BR /&gt;&lt;BR /&gt;I want to create a one row per person with the following:&lt;BR /&gt;&lt;BR /&gt;Id, month1-month24 that each contains the datenum variable for the corresponding month of the release from release_date. If there sre 2 or more on the same month, concatenate the datenum.&lt;BR /&gt;&lt;BR /&gt;So if i have this as input:&lt;BR /&gt;&lt;BR /&gt;Id.    Release_date.      Datenum&lt;BR /&gt;1     03/02/11               1245&lt;BR /&gt;2     01/01/11              3423&lt;BR /&gt;2     01/10/11               5432&lt;BR /&gt;&lt;BR /&gt;At the end, i want this&lt;BR /&gt;&lt;BR /&gt;Id.    month1.            month2.         month3. &lt;BR /&gt;1.                                                           1245&lt;BR /&gt;2.      34235432</description>
    <pubDate>Wed, 29 Mar 2017 01:10:39 GMT</pubDate>
    <dc:creator>malena</dc:creator>
    <dc:date>2017-03-29T01:10:39Z</dc:date>
    <item>
      <title>Concatenate if same month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-if-same-month/m-p/345212#M79385</link>
      <description>I have a dataset with id, release_dare, and a a variable called datenum&lt;BR /&gt;&lt;BR /&gt;Id $4&lt;BR /&gt;release_date mmddyy.&lt;BR /&gt;Datenum $4&lt;BR /&gt;&lt;BR /&gt;There are several rows per id since each can have many release dates.  &lt;BR /&gt;&lt;BR /&gt;I want to create a one row per person with the following:&lt;BR /&gt;&lt;BR /&gt;Id, month1-month24 that each contains the datenum variable for the corresponding month of the release from release_date. If there sre 2 or more on the same month, concatenate the datenum.&lt;BR /&gt;&lt;BR /&gt;So if i have this as input:&lt;BR /&gt;&lt;BR /&gt;Id.    Release_date.      Datenum&lt;BR /&gt;1     03/02/11               1245&lt;BR /&gt;2     01/01/11              3423&lt;BR /&gt;2     01/10/11               5432&lt;BR /&gt;&lt;BR /&gt;At the end, i want this&lt;BR /&gt;&lt;BR /&gt;Id.    month1.            month2.         month3. &lt;BR /&gt;1.                                                           1245&lt;BR /&gt;2.      34235432</description>
      <pubDate>Wed, 29 Mar 2017 01:10:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-if-same-month/m-p/345212#M79385</guid>
      <dc:creator>malena</dc:creator>
      <dc:date>2017-03-29T01:10:39Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate if same month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-if-same-month/m-p/345259#M79405</link>
      <description>&lt;P&gt;I am not sure what you are asking for.&amp;nbsp; Your WANT looks like multiple variables for month, but that does not make sense if concatenating.&amp;nbsp; The following concatenates the DATENUM values into one long character string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Id Release_date :mmddyy8. Datenum;
datalines;
1 03/02/11 1245
2 01/01/11 3423
2 01/10/11 5432
run;
proc sort data=have;
   by id release_date;
   run;
data want(keep=id monthnum);
   set have;
   by id;
   length monthnum $40;
   retain monthnum ;
   if first.id then monthnum=' ';
   monthnum=cats(monthnum,datenum);
   if last.id then output want;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you actually want the values in separate variables you may want to do a PROC TRANSPOSE.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Mar 2017 05:15:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-if-same-month/m-p/345259#M79405</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-03-29T05:15:22Z</dc:date>
    </item>
    <item>
      <title>Re: Concatenate if same month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Concatenate-if-same-month/m-p/345704#M79571</link>
      <description>&lt;P&gt;Malena,&lt;/P&gt;&lt;P&gt;I I understand you correctly, you want something like this:&lt;/P&gt;&lt;PRE&gt;data have;
input Id Release_date :mmddyy8. Datenum;
datalines;
1 03/02/11 1245
2 01/01/11 3423
2 01/10/11 5432
run;

proc sort data=have;
  by id Release_date;
run;

Data want;
  do until(last.id);
    set have;
    by id;
    array months (0:23) $20 month1-month24;
    if first.id then 
      date0=Release_date;
    idx=intck('month',Release_date,date0);
    if idx&amp;gt;23 then &lt;BR /&gt;      Error 'Date out of range, more than 24 months since first release!';
    else &lt;BR /&gt;      call cats(months(idx),Datenum);
    end;
  drop Release_date date0 idx;
run;&lt;/PRE&gt;&lt;P&gt;A couple of notes:&lt;/P&gt;&lt;P&gt;Instead of adding 1 to the array index IDX in every calculation, I zero-based the array (using (0:23) in the array definition). So, in the actual datastep, the month[0] is the month1 variable, etc.&lt;/P&gt;&lt;P&gt;I put in a check for array index out of range, just in case. SAS would also throws an errror message if you referenced the array with an index larger than 24, but I think it is better to get a reasonable informative error message.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Søren&lt;/P&gt;</description>
      <pubDate>Thu, 30 Mar 2017 10:45:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Concatenate-if-same-month/m-p/345704#M79571</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2017-03-30T10:45:43Z</dc:date>
    </item>
  </channel>
</rss>

