<?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: Need help combining and sorting data into one row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932358#M366773</link>
    <description>&lt;P&gt;And the most safe and easy way is using PROC SUMMARY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input ID $ Rating	Color $;
cards;
Flower-1	6	Purple
Flower-1	5	Green
Flower-2	1	Red
Flower-2	4	Indigo
Plant-1	10	Orange
Plant-1	2	Yellow
Plant-2	4	Blue
Plant-2	4	Violet
Plant-3	8	Black
Plant-3	6	White
;
proc sort data=have out=temp;
by id rating;
run;
proc sql noprint;
select max(n) into :n from
 (select count(*) as n from temp group by id);
quit;
proc summary data=temp ;
by id;
output out=want(drop=_type_ _freq_) idgroup(out[&amp;amp;n] (rating color)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 14 Jun 2024 01:40:58 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2024-06-14T01:40:58Z</dc:date>
    <item>
      <title>Need help combining and sorting data into one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932254#M366727</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a bunch of data that I would like to combine into one row and sort at the same time:&lt;/P&gt;
&lt;P&gt;have:&lt;/P&gt;
&lt;TABLE width="247"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="106"&gt;ID&lt;/T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gt;White&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like to combine similar IDs while sorting based on Rating:&lt;/P&gt;
&lt;P&gt;want:&lt;/P&gt;
&lt;TABLE width="375"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="106"&gt;ID&lt;/TD&gt;
&lt;TD width="64"&gt;Rating1&lt;/TD&gt;
&lt;TD width="77"&gt;Color1&lt;/TD&gt;
&lt;TD width="64"&gt;Rating2&lt;/TD&gt;
&lt;TD width="64"&gt;Color2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Flower-1&lt;/TD&gt;
&lt;TD&gt;5&lt;/TD&gt;
&lt;TD&gt;Green&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;TD&gt;Purple&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Flower-2&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;Red&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;Indigo&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Plant-1&lt;/TD&gt;
&lt;TD&gt;2&lt;/TD&gt;
&lt;TD&gt;Yellow&lt;/TD&gt;
&lt;TD&gt;10&lt;/TD&gt;
&lt;TD&gt;Orange&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Plant-2&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;Blue&lt;/TD&gt;
&lt;TD&gt;4&lt;/TD&gt;
&lt;TD&gt;Violet&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;Plant-3&lt;/TD&gt;
&lt;TD&gt;6&lt;/TD&gt;
&lt;TD&gt;White&lt;/TD&gt;
&lt;TD&gt;8&lt;/TD&gt;
&lt;TD&gt;Black&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is truly appreciated.&amp;nbsp; Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2024 18:52:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932254#M366727</guid>
      <dc:creator>arde</dc:creator>
      <dc:date>2024-06-13T18:52:50Z</dc:date>
    </item>
    <item>
      <title>Re: Need help combining and sorting data into one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932257#M366730</link>
      <description>&lt;P&gt;Is rating numeric or character?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sort by what (I hate guessing from examples as other data may affect the order)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you want a data set like that? What will you do with it? For most purposes the current data is more useful.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2024 19:13:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932257#M366730</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2024-06-13T19:13:40Z</dc:date>
    </item>
    <item>
      <title>Re: Need help combining and sorting data into one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932263#M366732</link>
      <description>The rating is numeric.&lt;BR /&gt;Sorting is to be done by the rating.  &lt;BR /&gt;I need the dataset to look like that because I have another dataset that I need to merge IDs.</description>
      <pubDate>Thu, 13 Jun 2024 19:27:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932263#M366732</guid>
      <dc:creator>arde</dc:creator>
      <dc:date>2024-06-13T19:27:54Z</dc:date>
    </item>
    <item>
      <title>Re: Need help combining and sorting data into one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932357#M366772</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input ID $ Rating	Color $;
cards;
Flower-1	6	Purple
Flower-1	5	Green
Flower-2	1	Red
Flower-2	4	Indigo
Plant-1	10	Orange
Plant-1	2	Yellow
Plant-2	4	Blue
Plant-2	4	Violet
Plant-3	8	Black
Plant-3	6	White
;
proc sort data=have out=temp;
by id rating;
run;
data temp;
 set temp;
 by id;
 if first.id then n=0;
 n+1;
run;
proc transpose data=temp out=temp2;
by id n;
var rating color;
run;
proc transpose data=temp2 out=want(drop=_NAME_);
by id ;
var col1;
id _name_ n;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Jun 2024 01:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932357#M366772</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-06-14T01:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Need help combining and sorting data into one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932358#M366773</link>
      <description>&lt;P&gt;And the most safe and easy way is using PROC SUMMARY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input ID $ Rating	Color $;
cards;
Flower-1	6	Purple
Flower-1	5	Green
Flower-2	1	Red
Flower-2	4	Indigo
Plant-1	10	Orange
Plant-1	2	Yellow
Plant-2	4	Blue
Plant-2	4	Violet
Plant-3	8	Black
Plant-3	6	White
;
proc sort data=have out=temp;
by id rating;
run;
proc sql noprint;
select max(n) into :n from
 (select count(*) as n from temp group by id);
quit;
proc summary data=temp ;
by id;
output out=want(drop=_type_ _freq_) idgroup(out[&amp;amp;n] (rating color)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Jun 2024 01:40:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932358#M366773</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-06-14T01:40:58Z</dc:date>
    </item>
    <item>
      <title>Re: Need help combining and sorting data into one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932359#M366774</link>
      <description>&lt;P&gt;I do not understand this sentence.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I need the dataset to look like that because I have another dataset that I need to merge IDs.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;Do you mean you have another dataset that also has the ID variable that you want MERGE with the current one?&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Why would it help the merge&amp;nbsp; to change the current dataset to have one observation per ID ?&amp;nbsp; Does the other dataset also have multiple observation per value of ID?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2024 01:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932359#M366774</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-06-14T01:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Need help combining and sorting data into one row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932367#M366779</link>
      <description>&lt;P&gt;If you are going to report this data, you dont't really need to combining manually. Based on dataset of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;, you can use across usage in proc report:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
  by id rating;
run;

data want;
  set have;
  by id rating;

  aseq+1;
  if first.id then aseq=1;
run;

proc report data=want;
  column id aseq,(rating color n);
  define id     /group;
  define aseq   /across;
  define rating /display;
  define color  /display;
  define n      /noprint;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Jun 2024 06:32:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-help-combining-and-sorting-data-into-one-row/m-p/932367#M366779</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2024-06-14T06:32:48Z</dc:date>
    </item>
  </channel>
</rss>

