<?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: Long to wide format with conditions in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/642553#M191707</link>
    <description>Thank you very much!</description>
    <pubDate>Fri, 24 Apr 2020 12:25:09 GMT</pubDate>
    <dc:creator>sandrube</dc:creator>
    <dc:date>2020-04-24T12:25:09Z</dc:date>
    <item>
      <title>Long to wide format with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/641714#M191321</link>
      <description>&lt;P&gt;I have the attached data set and I'm trying to convert it to wide format with conditions.There are patients who took drug1 and drug2. I want to see how many days each drug was taken and whether if was completed or discontinued. Whether it is completed or discontinued depend on the status on the last day. If it is completed on last day, it is completed. If it is discontinued on last day, it is discontinued. The final data would look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data new;
infile cards missover;
input Pt_id	drug1	drugs1_days	drug1_status $ drug2 drug2_days	drug2_status $	;
cards;
1	1	5	completed		1	1	completed	
2	1	2	discontinued		1	2	completed	
3	1	1	completed		0	.	.	
4	1	3	completed		1	2	discontinued	
5	1	4	completed		0	.	.	
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thank you for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Apr 2020 18:53:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/641714#M191321</guid>
      <dc:creator>sandrube</dc:creator>
      <dc:date>2020-04-21T18:53:58Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/641821#M191367</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*sort*/
proc sort data=have;
by Pt_id drugs start_date end_date;
run;

/*calculate number of days, then sum by Pt_id and drug*/
proc sql;
create table days as 
select Pt_id, drugs, 
sum(intck('day',start_date,end_date)+1) as days 
from have
group by Pt_id, drugs;
quit;

/*keep last status*/
data stat (keep=Pt_id drugs stat where=(stat ne '')); 
set have;
by Pt_id drugs start_date end_date;
format stat $12.;

if last.drugs then stat=status; 
if substr(stat,1,3)="dis" then stat="discontinued";
run;

/*long to wide*/
%macro trns(var);
proc transpose  
data=&amp;amp;var 
out=trns_&amp;amp;var
prefix=&amp;amp;var._
;

by Pt_id;
id drugs;
var &amp;amp;var;
run;
%mend trns;

/*transpose one at a time*/
%trns(days);
%trns(stat);

/*combine*/
proc sql;
create table want as 
select 
t1.Pt_id,
(case when t1.days_drug1 ne . then 1 else 0 end) as drug1,
t1.days_drug1 as drug1_days,
t2.stat_drug1 as drug1_status,
(case when t1.days_drug2 ne . then 1 else 0 end) as drug2,
t1.days_drug2 as drug2_days,
t2.stat_drug2 as drug2_status 
from trns_days t1 
inner join trns_stat t2 
on t1.Pt_id = t2.Pt_id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Apr 2020 06:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/641821#M191367</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2020-04-22T06:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/641875#M191402</link>
      <description>&lt;P&gt;If you want the wide format for some sort of reporting, then PROC REPORT can work with your data in the format it is in, and produce a report, without YOU specifically programming a conversion of the data set to a wide format.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For just about anything else you want to do in SAS, the wide format is less effective and requires more coding than leaving the data in the long format, and so is not advised.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Apr 2020 11:16:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/641875#M191402</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-04-22T11:16:09Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/642552#M191706</link>
      <description>ok, thank you!</description>
      <pubDate>Fri, 24 Apr 2020 12:24:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/642552#M191706</guid>
      <dc:creator>sandrube</dc:creator>
      <dc:date>2020-04-24T12:24:52Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format with conditions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/642553#M191707</link>
      <description>Thank you very much!</description>
      <pubDate>Fri, 24 Apr 2020 12:25:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide-format-with-conditions/m-p/642553#M191707</guid>
      <dc:creator>sandrube</dc:creator>
      <dc:date>2020-04-24T12:25:09Z</dc:date>
    </item>
  </channel>
</rss>

