<?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: Keep all variables from dataset when using proc summary in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790448#M32488</link>
    <description>&lt;P&gt;If AGE and/or COUNT is constant for an ID then you could include it in the BY statement. But that does not seem to be the case. Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To drop variables use the DROP= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
  by id;
  output
    out=want(drop=age2-age&amp;amp;n count2-count&amp;amp;n
             rename=(age1=age count1=count))
    idgroup(out[&amp;amp;n] (visit_num age diag treatment count)=)
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If AGE and COUNT a numeric you could calculate a statistic for them instead.&amp;nbsp; Perhaps the minimum age and the sum of the counts?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
  by id;
  var age count;
  output
    out=want
   min(age)= sum(count)=
    idgroup(out[&amp;amp;n] (visit_num diag treatment )=)
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 17 Jan 2022 01:39:59 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-01-17T01:39:59Z</dc:date>
    <item>
      <title>Keep all variables from dataset when using proc summary</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790444#M32486</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a long dataset and am using proc summary and sql to transform it to a wide dataset. I'm having trouble adapting the following code to retain all of my variables in my long dataset in the new wide dataset. Example of my code is below. For my age and count variables, I would like to retain them but if I include them in the IDGROUP statement, then SAS creates multiple age and count columns in my wide dataset (I would just like one, see desired output below).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id visit_num diag$ age treatment count;
cards;
01 1 diabetes 42 1 1
02 1 stroke 78 1 2
02 2 cancer . 2 2
02 3 stroke . . 2
03 1 stroke 66 2 2
03 2 copd . 1 2
03 3 . . . 2
;
run;

proc sql noprint;
select max(n) into :n
 from (select count(*) as n from have group by id);
quit;
proc summary data=have;
by id;
output out=want idgroup(out[&amp;amp;n] (visit_num diag treatment)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Desired output&lt;/STRONG&gt;:(Note, in my actual dataset I have ~40 variables similar to count and age, where I would like just one column for each of these variables in my wide dataset).&lt;/P&gt;
&lt;TABLE border="1" width="620px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="92.5167px" height="30px"&gt;Visit_Num1&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;Visit_Num2&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;Visit_Num3&lt;/TD&gt;
&lt;TD width="40px" height="30px"&gt;Age&lt;/TD&gt;
&lt;TD width="92.25px"&gt;Treatment1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;Treatment2&lt;/TD&gt;
&lt;TD width="92.25px"&gt;Treatment3&lt;/TD&gt;
&lt;TD width="40px"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;01&lt;/TD&gt;
&lt;TD width="92.5167px" height="30px"&gt;diabetes&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px" height="30px"&gt;42&lt;/TD&gt;
&lt;TD width="92.25px"&gt;1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;02&lt;/TD&gt;
&lt;TD width="92.5167px" height="30px"&gt;stroke&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;cancer&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;stroke&lt;/TD&gt;
&lt;TD width="40px" height="30px"&gt;78&lt;/TD&gt;
&lt;TD width="92.25px"&gt;1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;2&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px"&gt;03&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;stroke&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;copd&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;66&lt;/TD&gt;
&lt;TD width="92.25px"&gt;2&lt;/TD&gt;
&lt;TD width="92.25px"&gt;1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;</description>
      <pubDate>Sun, 16 Jan 2022 23:26:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790444#M32486</guid>
      <dc:creator>monsterpie</dc:creator>
      <dc:date>2022-01-16T23:26:26Z</dc:date>
    </item>
    <item>
      <title>Re: Keep all variables from dataset when using proc summary</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790447#M32487</link>
      <description>&lt;P&gt;Without re-writing the existing code&amp;nbsp; ( which works just fine for most part). I would suggest adding a step to re-merging the age and count variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc sql noprint;
	select max(n) into :n from (select count(*) as n from have group by id);
quit;

proc summary data=have;
	by id;
	output out=want idgroup(out[&amp;amp;n] (visit_num diag treatment)=);
run;


data final;
	merge want have(where=(age ne .) keep=id age  count );
	by id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jan 2022 00:29:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790447#M32487</guid>
      <dc:creator>r_behata</dc:creator>
      <dc:date>2022-01-17T00:29:40Z</dc:date>
    </item>
    <item>
      <title>Re: Keep all variables from dataset when using proc summary</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790448#M32488</link>
      <description>&lt;P&gt;If AGE and/or COUNT is constant for an ID then you could include it in the BY statement. But that does not seem to be the case. Why?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To drop variables use the DROP= dataset option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
  by id;
  output
    out=want(drop=age2-age&amp;amp;n count2-count&amp;amp;n
             rename=(age1=age count1=count))
    idgroup(out[&amp;amp;n] (visit_num age diag treatment count)=)
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If AGE and COUNT a numeric you could calculate a statistic for them instead.&amp;nbsp; Perhaps the minimum age and the sum of the counts?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=have;
  by id;
  var age count;
  output
    out=want
   min(age)= sum(count)=
    idgroup(out[&amp;amp;n] (visit_num diag treatment )=)
  ;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jan 2022 01:39:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790448#M32488</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-17T01:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Keep all variables from dataset when using proc summary</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790553#M32496</link>
      <description>&lt;P&gt;This gets you pretty close, except it's not coded as VISIT_NUM but DIAG1 - DIAG3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id visit_num diag$ age treatment count;
cards;
01 1 diabetes 42 1 1
02 1 stroke 78 1 2
02 2 cancer . 2 2
02 3 stroke . . 2
03 1 stroke 66 2 2
03 2 copd . 1 2
03 3 . . . 2
;
run;

proc sql noprint;
select max(n) into :n
 from (select count(*) as n from have group by id);
quit;
proc summary data=have;
by id;
ID Age COUNT;
output out=want (drop=_:) idgroup(out[&amp;amp;n] (diag treatment)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Reeza_0-1642440129462.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/67465iBAB86B18F7780275/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Reeza_0-1642440129462.png" alt="Reeza_0-1642440129462.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/318138"&gt;@monsterpie&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a long dataset and am using proc summary and sql to transform it to a wide dataset. I'm having trouble adapting the following code to retain all of my variables in my long dataset in the new wide dataset. Example of my code is below. For my age and count variables, I would like to retain them but if I include them in the IDGROUP statement, then SAS creates multiple age and count columns in my wide dataset (I would just like one, see desired output below).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input id visit_num diag$ age treatment count;
cards;
01 1 diabetes 42 1 1
02 1 stroke 78 1 2
02 2 cancer . 2 2
02 3 stroke . . 2
03 1 stroke 66 2 2
03 2 copd . 1 2
03 3 . . . 2
;
run;

proc sql noprint;
select max(n) into :n
 from (select count(*) as n from have group by id);
quit;
proc summary data=have;
by id;
output out=want idgroup(out[&amp;amp;n] (visit_num diag treatment)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;Desired output&lt;/STRONG&gt;:(Note, in my actual dataset I have ~40 variables similar to count and age, where I would like just one column for each of these variables in my wide dataset).&lt;/P&gt;
&lt;TABLE border="1" width="620px"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;ID&lt;/TD&gt;
&lt;TD width="92.5167px" height="30px"&gt;Visit_Num1&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;Visit_Num2&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;Visit_Num3&lt;/TD&gt;
&lt;TD width="40px" height="30px"&gt;Age&lt;/TD&gt;
&lt;TD width="92.25px"&gt;Treatment1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;Treatment2&lt;/TD&gt;
&lt;TD width="92.25px"&gt;Treatment3&lt;/TD&gt;
&lt;TD width="40px"&gt;Count&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;01&lt;/TD&gt;
&lt;TD width="92.5167px" height="30px"&gt;diabetes&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px" height="30px"&gt;42&lt;/TD&gt;
&lt;TD width="92.25px"&gt;1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" height="30px"&gt;02&lt;/TD&gt;
&lt;TD width="92.5167px" height="30px"&gt;stroke&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;cancer&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;stroke&lt;/TD&gt;
&lt;TD width="40px" height="30px"&gt;78&lt;/TD&gt;
&lt;TD width="92.25px"&gt;1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;2&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px"&gt;03&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;stroke&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;copd&lt;/TD&gt;
&lt;TD width="92.5167px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;66&lt;/TD&gt;
&lt;TD width="92.25px"&gt;2&lt;/TD&gt;
&lt;TD width="92.25px"&gt;1&lt;/TD&gt;
&lt;TD width="92.25px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="40px"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jan 2022 17:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Keep-all-variables-from-dataset-when-using-proc-summary/m-p/790553#M32496</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-17T17:23:02Z</dc:date>
    </item>
  </channel>
</rss>

