<?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 in Statistical Procedures</title>
    <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/642555#M30742</link>
    <description>Thank you very much!</description>
    <pubDate>Fri, 24 Apr 2020 12:27:03 GMT</pubDate>
    <dc:creator>sandrube</dc:creator>
    <dc:date>2020-04-24T12:27:03Z</dc:date>
    <item>
      <title>Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641808#M30688</link>
      <description>&lt;P&gt;I have patients with multiple rows and variables diabetes (1=yes and 2=no) and hypertension (1=yes and 2=no). For each patient, if it is 1 in any row, it should be considered as 1. Please see the initial data set:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input pt_id	diabetes hypertension;
datalines;
1 0 0
1 1 0
1 1 0
1 0 0
2 0 0
2 0 0
2 0 0
2 0 0
3 1 1
3 0 1
3 0 1
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Final data set should look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input pt_id	diabetes hypertension;
datalines;
1 1 0
2 0 0
3 1 1
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;</description>
      <pubDate>Wed, 22 Apr 2020 04:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641808#M30688</guid>
      <dc:creator>sandrube</dc:creator>
      <dc:date>2020-04-22T04:05:52Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641820#M30690</link>
      <description>&lt;P&gt;Try proc sql -- you desire the max of each flag for each pt_id:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input pt_id diabetes hypertension;
	datalines;
1 0 0
1 1 0
1 1 0
1 0 0
2 0 0
2 0 0
2 0 0
2 0 0
3 1 1
3 0 1
3 0 1
;
run;

data desired;
	input pt_id diabetes hypertension;
	datalines;
1 1 0
2 0 0
3 1 1
;
run;

*Solution;
proc sql;
	create table want as 
	select 
		pt_id
		, max(diabetes) as diabetes
		, max(hypertension) as hypertension 
	from have 
	group by pt_id 
	order by pt_id
	;
quit;

*Compare want to desired;
proc compare data=desired comp=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Apr 2020 06:01:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641820#M30690</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-04-22T06:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641826#M30691</link>
      <description>&lt;P&gt;Data and description don't match, please clarify: do you have 1=yes and 0=no or 1=yes and 2=no?&lt;/P&gt;
&lt;P&gt;The follow step works if&amp;nbsp; 0=no:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	set have;
	by pt_id;
	
	retain _diabetes _hypertension 0;
	
	if first.pt_id then do;
		_diabetes = diabetes;
		_hypertension = hypertension;
	end;
	else do;
		_diabetes = diabetes or _diabetes;
		_hypertension = hypertension or _hypertension;
	end;
	
	if last.pt_id then do;		
		output;
	end;
	
	drop diabetes hypertension;
	rename 
		_diabetes = diabetes
		_hypertension = hypertension
	;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Apr 2020 06:59:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641826#M30691</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-04-22T06:59:50Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641843#M30692</link>
      <description>&lt;P&gt;The simple code below will work&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have ;
by pt_id descending diabetes descending hypertension;
run;

data want;
set have;
by pt_id descending diabetes descending hypertension;
if first.pt_id;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Apr 2020 08:27:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/641843#M30692</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2020-04-22T08:27:10Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/642555#M30742</link>
      <description>Thank you very much!</description>
      <pubDate>Fri, 24 Apr 2020 12:27:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/642555#M30742</guid>
      <dc:creator>sandrube</dc:creator>
      <dc:date>2020-04-24T12:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/642556#M30743</link>
      <description>Than you very much!</description>
      <pubDate>Fri, 24 Apr 2020 12:27:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/642556#M30743</guid>
      <dc:creator>sandrube</dc:creator>
      <dc:date>2020-04-24T12:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/642661#M30748</link>
      <description>&lt;P&gt;The question subject is misstated as "Long to wide".&amp;nbsp; The question is really one of "Max over Group", or more generically "Result over Group"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The result over group for this question "presence within group".&lt;/P&gt;
&lt;P&gt;A "DOW" loop is very effective for processing data sorted into BY groups, and outputting one row per group. (The technique can also be used to apply the computed result to each row in the group).&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want(keep=pt_id diabetes hypertension);
  do until (last.pt_id);
    SET have (rename=(diabetes=_dia hypertension=_htn));
    BY pt_id;

    diabetes = _dia OR diabetes;
    hypertension = _htn OR hypertension;
  end;
run;
&lt;/PRE&gt;
&lt;P&gt;The secret to the DOW loop is placing the SET and BY statements inside a DO LOOP.&amp;nbsp; An &lt;STRONG&gt;implicit&amp;nbsp;&lt;/STRONG&gt;OUTPUT occurs at the end of the step with the desired computed results.&amp;nbsp; The rename= is necessary because the programmer wants the aggregate result variables (diabetes/hypertension) to be the same name as the individual flag variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 16:36:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/642661#M30748</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-04-24T16:36:33Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide format</title>
      <link>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/643079#M30787</link>
      <description>Thank you very much!!!</description>
      <pubDate>Sun, 26 Apr 2020 17:21:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/Statistical-Procedures/Long-to-wide-format/m-p/643079#M30787</guid>
      <dc:creator>sandrube</dc:creator>
      <dc:date>2020-04-26T17:21:22Z</dc:date>
    </item>
  </channel>
</rss>

