<?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: first date with label in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648867#M194458</link>
    <description>Hi PaigeMiller, there are plenty of records 30+, I just listed two to simplify the idea</description>
    <pubDate>Tue, 19 May 2020 14:56:34 GMT</pubDate>
    <dc:creator>lillymaginta</dc:creator>
    <dc:date>2020-05-19T14:56:34Z</dc:date>
    <item>
      <title>first date with label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648322#M194175</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input id$2. start drug;
attrib start format =date9. informat=date9.;

datalines;
1 01JAN2015 1
1 18FEB2015  2
2 01jan2016  2
2 01apr2016  1
3 01JAN2015 1
3 01JAN2015  2&lt;BR /&gt;4 01jan2017  2
4 01apr2017  2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am trying to find the first of each drug per id. Retain the first as "first" and indicate the drug name under "drug". And return the start of the other drug as "comp". If both drugs started the same day then output "N".&amp;nbsp;&lt;/P&gt;&lt;P&gt;output&lt;/P&gt;&lt;P&gt;id&amp;nbsp; &amp;nbsp;first&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;comp&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; drug&lt;/P&gt;&lt;P&gt;1&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;01JAN2015&amp;nbsp;&amp;nbsp;&amp;nbsp;18FEB2015&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;2&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;01JAN2016&amp;nbsp;&amp;nbsp;&amp;nbsp;01apr2016&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;3&amp;nbsp;&amp;nbsp;&lt;CODE class=" language-sas"&gt;01JAN2015&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;01JAN2015&amp;nbsp;&amp;nbsp;N&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;4&amp;nbsp;&lt;CODE class=" language-sas"&gt;01jan2017&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2020 17:13:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648322#M194175</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2020-05-16T17:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: first date with label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648339#M194181</link>
      <description>&lt;P&gt;If there are always two records for each ID then PROC SUMMARY ... wait, I don't want to assume ... are there always going to be 2 records for each ID?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why is there no COMP for ID = 4?&lt;/P&gt;</description>
      <pubDate>Sat, 16 May 2020 20:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648339#M194181</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-05-16T20:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: first date with label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648374#M194198</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/72105"&gt;@lillymaginta&lt;/a&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;Here is an attempt to achieve this. Please review the way to compute COMP date.&lt;/P&gt;
&lt;P&gt;From your explanations, I understand that there are 3 cases:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;CASE1: the subject takes only one drug (id:4) -&amp;gt; we consider that comp=. has it it is ongoing&lt;/LI&gt;
&lt;LI&gt;CASE2: the subject takes multiples drugs all at the same date and no next drug (id:3) -&amp;gt; we consider that comp= start&lt;/LI&gt;
&lt;LI&gt;CASE3: the subject takes one or more drugs&amp;nbsp;following the initial one (id:1, 2) -&amp;gt; we consider that comp=&amp;nbsp;first intake date of the next drug&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt; Best,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	input id$2. start drug;
	attrib start format=date9. informat=date9.;
	datalines;
1 01JAN2015 1
1 18FEB2015  2
2 01jan2016  2
2 01apr2016  1
3 01JAN2015 1
3 01JAN2015  2
4 01jan2017  2
4 01apr2017  2
;
run;

proc sort data=test out=test_sorted;
	by id start;
run;

/*** If both initial drugs started the same day then output "N" */

data test2;
	set test_sorted;
	by id start;
	
	if first.start then drugc = put(drug, 1.);
	else drugc = "N";
	
	drop drug;
	rename drugc=drug;
	
	if last.start then output;
run;

/*** Flag first drug and first drug intake */

data test3;
	set test2;
	by id start;	
	if first.id then flag_drug1_intake1=1;
	else flag_drug1_intake1=0;
run;

proc sql;
	create table test4 as
	select a.id, a.start, a.drug, case when b.flag_drug1_intake1 then 1 else 0 end as flag_drug1, a.flag_drug1_intake1
	from test3 as a
		 left join
		 (select * from test3 where flag_drug1_intake1=1) as b
	on a.id = b.id and a.drug = b.drug
	order by a.id, a.start;
quit;

/*** Identify COMP date */
	
	/*** CASE1: ID with ongoing first 'unique' drug
		 COMP = .*/
	
	proc sql;
		create table ongoing_unique as
		select distinct id, . as comp
		from test4
		group by id
		having count(distinct flag_drug1)=1 and drug ne "N";
	quit;

	/*** CASE2: ID with ongoing first 'multiples' drug
		 COMP = start */
	proc sql;
		create table ongoing_multiple as
		select distinct id, start as comp
		from test4
		group by id
		having count(distinct flag_drug1)=1 and drug="N";
	quit;

	/*** CASE3: ID with other drug(s) following the initial one
		 COMP = first intake date of the next drug */
	proc sql;
		create table ID_other as
		select *
		from test4
		group by id
		having count(distinct flag_drug1)&amp;gt;1
		order by ID, flag_drug1, start;
	quit;
		/* Retrieve COMP date */
		data other;
			set ID_other;
			by ID flag_drug1 start ;
			if first.flag_drug1 and flag_drug1 ne 1;
			keep ID start;
			rename start = comp;
		run;

/*** Merge all couples ID - COMP */
data want_comp;
	merge ongoing_unique ongoing_multiple other;
	by ID comp;
run;

/*** FINAL MERGE */
data want;
	merge test4 (where=(flag_drug1_intake1=1)) want_comp;
	by id;
	drop flag:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture d’écran 2020-05-17 à 11.42.54.png" style="width: 200px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/39535i66023659F0BD3315/image-size/small?v=v2&amp;amp;px=200" role="button" title="Capture d’écran 2020-05-17 à 11.42.54.png" alt="Capture d’écran 2020-05-17 à 11.42.54.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 17 May 2020 09:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648374#M194198</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-05-17T09:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: first date with label</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648867#M194458</link>
      <description>Hi PaigeMiller, there are plenty of records 30+, I just listed two to simplify the idea</description>
      <pubDate>Tue, 19 May 2020 14:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-date-with-label/m-p/648867#M194458</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2020-05-19T14:56:34Z</dc:date>
    </item>
  </channel>
</rss>

