<?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 Adding rows to long format data in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904054#M40362</link>
    <description>&lt;P&gt;Closed&lt;/P&gt;</description>
    <pubDate>Tue, 21 Nov 2023 14:17:33 GMT</pubDate>
    <dc:creator>hellorc</dc:creator>
    <dc:date>2023-11-21T14:17:33Z</dc:date>
    <item>
      <title>Adding rows to long format data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904054#M40362</link>
      <description>&lt;P&gt;Closed&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 14:17:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904054#M40362</guid>
      <dc:creator>hellorc</dc:creator>
      <dc:date>2023-11-21T14:17:33Z</dc:date>
    </item>
    <item>
      <title>Re: Adding rows to long format data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904057#M40364</link>
      <description>&lt;P&gt;Here you go&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
  input id start stop @@;
  informat start stop mmddyy10.;
  format start stop mmddyy10.;
  datalines;
1 01/01/2019 02/01/2019 
1 02/01/2019 03/01/2019 
1 03/01/2019 04/01/2019
1 04/01/2019 05/01/2019
1 05/01/2019 06/01/2019
2 04/01/2019 05/01/2019
2 05/01/2019 06/01/2019
;
run;

data have2;
  input id surgery1 surgery2 @@;
  informat surgery1 surgery2 mmddyy10.;
  format surgery1 surgery2 mmddyy10.;
  datalines;
1 02/15/2019 04/10/2019
;
run;

/* option 1: if your source data is already sorted by id and start/surgery */
data want_1;
  set have1 have2(rename=(surgery1=start surgery2=stop));
  by id start;
run;

/* option 2: if the source data is not already sorted */
data want_2;
  set have1 have2(rename=(surgery1=start surgery2=stop));
run;
proc sort data=want_2;
  by id start;
run;

/* option 3: SQL Union */
proc sql feedback;
  create table want_3 as
    select 
      t1.id
      , t1.start
      , t1.stop
    from WORK.HAVE1 t1
    union all
    select 
      t2.id
      , t2.surgery1
      , t2.surgery2
    from WORK.HAVE2 t2
    order by id, start
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Nov 2023 08:42:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904057#M40364</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-21T08:42:16Z</dc:date>
    </item>
    <item>
      <title>Re: Adding rows to long format data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904074#M40366</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
  input id start stop @@;
  informat start stop mmddyy10.;
  format start stop mmddyy10.;
  datalines;
1 01/01/2019 02/01/2019 
1 02/01/2019 03/01/2019 
1 03/01/2019 04/01/2019
1 04/01/2019 05/01/2019
1 05/01/2019 06/01/2019
2 04/01/2019 05/01/2019
2 05/01/2019 06/01/2019
;
run;

data have2;
  input id surgery1 surgery2 @@;
  informat surgery1 surgery2 mmddyy10.;
  format surgery1 surgery2 mmddyy10.;
  datalines;
1 02/15/2019 04/10/2019
;
run;

data temp;
set have1;
n+1;
do date=start to stop;
 output;
end;
keep id date n;
run;
proc transpose data=have2 out=temp2;
by id;
var surgery:;
run;

proc sort data=temp;by id date;run;

data want;
 set temp temp2(keep=id col1 rename=(col1=date) in=inb );
 by id date;
 flag=inb;
 if not missing(date);
 format date mmddyy10.;
run;
data want;
 update want(obs=0) want;
 by id;
 output;
run;
data want;
 set want;
 by id n notsorted;
 if first.id then do;group=0;group2=0;end;
 if first.n then group+1;
 if flag then group2+1;
run;
proc sql;
create table final_want as
select id,group,group2,min(date) as start format=mmddyy10.,max(date) as stop format=mmddyy10.
 from want
  group by id,group,group2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Nov 2023 12:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904074#M40366</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-11-21T12:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: Adding rows to long format data</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904129#M40379</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/431043"&gt;@hellorc&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Closed&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Why did you delete your question after you received answers?&lt;/P&gt;
&lt;P&gt;The forum Q&amp;amp;A is also useful to find solutions to questions that have been discussed already - but that only works if you don't delete the question.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 15:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Adding-rows-to-long-format-data/m-p/904129#M40379</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-11-21T15:49:53Z</dc:date>
    </item>
  </channel>
</rss>

