<?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: Flattening/Transposing a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783834#M250029</link>
    <description>&lt;P&gt;&amp;nbsp;I do not recommend doing this. This structure is typically not useful and your current form is usually the preferred.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are you doing that you think you need that form?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 03 Dec 2021 04:16:22 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-12-03T04:16:22Z</dc:date>
    <item>
      <title>Flattening/Transposing a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783830#M250027</link>
      <description>&lt;P&gt;&lt;FONT size="2"&gt;I'm am trying to take the following dataset (for example):&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;&lt;FONT size="2"&gt;&lt;CODE class=""&gt;data test;
input ID Name $ Topic A1 A2;
datalines;
1 Alex 1 1 5
1 Alex 2 5 1
1 Bob 2 5 1
1 Smith 4 1 5
2 Greg 1 1 1
2 Greg 2 1 1
3 Joe 3 1 5
3 Joe 4 1 5
run;&lt;/CODE&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;P&gt;&lt;FONT size="2"&gt;And collapse it&amp;nbsp; down by ID and Name so that it looks like this (the table has been cut off but you get the idea hopefully).&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ALEXIA1_1-1638502246664.png" style="width: 452px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66365i48E540A3805C6832/image-dimensions/452x61?v=v2" width="452" height="61" role="button" title="ALEXIA1_1-1638502246664.png" alt="ALEXIA1_1-1638502246664.png" /&gt;&lt;/span&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size="2"&gt;I am relatively new to SAS so I'm not sure what the best approach would be. I've tried transposing with no luck, as well as creating blank columns and moving the data (however this is very inefficient if you have a big dataset). Any recommendations are welcome!&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Dec 2021 03:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783830#M250027</guid>
      <dc:creator>ALEXIA1</dc:creator>
      <dc:date>2021-12-03T03:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Flattening/Transposing a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783832#M250028</link>
      <description>&lt;P&gt;PROC SUMMARY will do a good job.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your example the code could look like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test;
  by id name;
  output out=want(drop=_type_) idgroup( out[2] (topic a1 a2)=);
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;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs    ID    Name     _FREQ_    Topic_1    Topic_2    A1_1    A1_2    A2_1    A2_2

 1      1    Alex        2         1          2         1       5       5       1
 2      1    Bob         1         2          .         5       .       1       .
 3      1    Smith       1         4          .         1       .       5       .
 4      2    Greg        2         1          2         1       1       1       1
 5      3    Joe         2         3          4         1       1       5       5
&lt;/PRE&gt;
&lt;P&gt;I told it to create only [2] variables because I could see that the max number of rows was only two.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you could use code to count that maximum number needed and put that into a macro variable instead.&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(count) into :n_reps trimmed
from (select id,name,count(*) as count
      from test
      group by id,name )
;
quit;

proc summary data=test;
  by id name;
  output out=want(drop=_type_) idgroup( out[&amp;amp;n_reps] (topic a1 a2)=);
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If the data is not sorted then use a CLASS statement instead of the BY statement and add the NWAY option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=test nway;
  class id name;
  output out=want(drop=_type_) idgroup( out[&amp;amp;n_reps] (topic a1 a2)=);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Dec 2021 04:12:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783832#M250028</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-12-03T04:12:51Z</dc:date>
    </item>
    <item>
      <title>Re: Flattening/Transposing a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783834#M250029</link>
      <description>&lt;P&gt;&amp;nbsp;I do not recommend doing this. This structure is typically not useful and your current form is usually the preferred.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are you doing that you think you need that form?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Dec 2021 04:16:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783834#M250029</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-12-03T04:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: Flattening/Transposing a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783835#M250030</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Without doing any transposing, PROC REPORT can give you a report using TOPIC as an ACROSS item on the report, as shown below:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1638504809970.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/66366i2CDF066D2C21E3B1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1638504809970.png" alt="Cynthia_sas_0-1638504809970.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I know the structure of the report isn't exactly what you showed as the desired output. I wasn't really clear on what you needed as output -- a new data file or a report. But this is what you can get by default with your existing structure and without renaming variables. If you need to rename variables and change the structure, then I would probably choose a DATA step program. I have to admit, I don't quite understand how one ID can have 3 different name values, as shown for ID 1 -- so I suspect this is fake data and that your real data has some other relationship between ID and Name, where the multiple rows for a single ID makes sense.&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Dec 2021 04:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783835#M250030</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2021-12-03T04:17:26Z</dc:date>
    </item>
    <item>
      <title>Re: Flattening/Transposing a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783836#M250031</link>
      <description>&lt;P&gt;Before going on with this, what will the resulting data set be used for?&lt;/P&gt;
&lt;P&gt;What is the maximum number times any of the Id Name combinations ever appear? By ever I mean not just in this data set but in any likely to occur in the future data sets.&lt;/P&gt;
&lt;P&gt;Does the value of Topic &lt;STRONG&gt;ever &lt;/STRONG&gt;duplicate within an Id Name combination?&lt;/P&gt;
&lt;P&gt;Also, is the actual order of all the variable columns important? I have to ask because some possible solutions make that somewhat difficult to do and require some code that is a bit ugly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Warning: the form that you propose is almost impossible to do most types of analysis with, graph, or probably even make nice appearing tables from.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Dec 2021 04:18:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783836#M250031</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-12-03T04:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: Flattening/Transposing a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783897#M250068</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
input ID Name $ Topic A1 A2;
datalines;
1 Alex 1 1 5
1 Alex 2 5 1
1 Bob 2 5 1
1 Smith 4 1 5
2 Greg 1 1 1
2 Greg 2 1 1
3 Joe 3 1 5
3 Joe 4 1 5
;
run;



proc sql noprint;
select distinct catt('test(where=(topic_',topic,'=',topic,') rename=(topic=topic_',topic,' a1=a1_',topic,' a2=a2_',topic,'))')
       into :merge separated by ' '
 from test;
quit;

data want;
 merge &amp;amp;merge.;
 by id name;
run;

proc stdize data=want out=final_want missing=0 reponly;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Dec 2021 12:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/783897#M250068</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2021-12-03T12:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Flattening/Transposing a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/784018#M250113</link>
      <description>Hi ballardw, the dataset will later be merged to a larger dataset whereby one row exists for each ID/name.&lt;BR /&gt;The max number of times a given ID/name combination can appear in the dataset above and future datasets is 4. The value of Topic never duplicates for a ID/Name combination. And, the order of the columns is not important.&lt;BR /&gt;&lt;BR /&gt;I'm expecting the code to be a big ugly but hopefully this information helps! Thanks</description>
      <pubDate>Fri, 03 Dec 2021 21:28:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Flattening-Transposing-a-dataset/m-p/784018#M250113</guid>
      <dc:creator>ALEXIA1</dc:creator>
      <dc:date>2021-12-03T21:28:22Z</dc:date>
    </item>
  </channel>
</rss>

