<?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 to transpose data with multiple variables in a better way in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598620#M172648</link>
    <description>&lt;P&gt;Post your data as self-contained data steps, using datalines, that we can cut-and-paste into SAS.&amp;nbsp; Use Insert SAS Code (running man icon) to do so.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your mangled cut-and-paste of your input and output data makes it hard for us to help you.&lt;/P&gt;</description>
    <pubDate>Wed, 23 Oct 2019 06:40:05 GMT</pubDate>
    <dc:creator>ScottBass</dc:creator>
    <dc:date>2019-10-23T06:40:05Z</dc:date>
    <item>
      <title>Dynamically sort the column variables in the desired order without PROC SQL manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598613#M172647</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm very new to SAS programming and I got a requirement to transpose multi variables from the csv file. I found a solution in SAS community through PROC SUMMARY and i somehow manipulated the order of output variables through PROC SQL as per the requirement.&lt;BR /&gt;But i would like to know if it's possible to produce this output without manipulation at PROC SQL step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Version: SAS 9.4&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;Input Data:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RANK6_CODE|RANK_CODE|RANK_DESC|RANK_KEY&lt;/P&gt;&lt;P&gt;1|467|Sample_Description0|Rank_1&lt;/P&gt;&lt;P&gt;1|120|Sample_Description1|Rank_2&lt;/P&gt;&lt;P&gt;1|456|Sample_Description2|Rank_3&lt;/P&gt;&lt;P&gt;1|778|Sample_Description3|Rank_4&lt;/P&gt;&lt;P&gt;1|780|Sample_Description4|Rank_5&lt;/P&gt;&lt;P&gt;1|402|Sample_Description5|Rank_6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Output Data:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RANK6_CODE|RANK6_DESC|RANK5_CODE|RANK5_DESC|RANK4_CODE|RANK4_DESC|RANK3_CODE|RANK3_DESC|RANK2_CODE|RANK2_DESC|RANK1_CODE|RANK1_DESC&lt;BR /&gt;402|Sample_Description5|780|Sample_Description4|778|Sample_Description3|456|Sample_Description2|120|Sample_Description1|467|Sample_Description&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Note:&amp;nbsp;&lt;/STRONG&gt;I have added the sample values through Datalines as requested.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;STRONG&gt;Code:&lt;/STRONG&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA PROJECT.IMPORT;
INPUT @1 RANK6_CODE RANK_CODE $3-5 RANK_DESC $7-26 RANK_KEY $27-32;
DATALINES;
1 467 Sample_Description0 Rank_1 
1 120 Sample_Description1 Rank_2 
1 456 Sample_Description2 Rank_3 
1 778 Sample_Description3 Rank_4 
1 780 Sample_Description4 Rank_5 
1 402 Sample_Description5 Rank_6 
3 467 Sample_Description0 Rank_1 
3 120 Sample_Description1 Rank_2 
3 456 Sample_Description2 Rank_3 
3 778 Sample_Description3 Rank_4 
3 780 Sample_Description4 Rank_5 
3 402 Sample_Description5 Rank_6
;
RUN;

proc sort data=PROJECT.IMPORT; 
by RANK6_CODE; 
run;


proc summary data=PROJECT.IMPORT; 
CLASS RANK6_CODE; 
OUTPUT OUT=wide(drop=_:)idgroup(out[6](RANK_DESC RANK_CODE )=); 
/*Manually hard coded the number of Rank occurrence to 6*/&lt;BR /&gt;/*I can pass the value of occurrence by finding out the maximum count and assigning it to a local variable in OUT option - out[&amp;amp;variable]*/
run; 

PROC PRINT DATA=work.wide;
RUN;


PROC SQL;
CREATE TABLE PROJECT.REQ1 AS
SELECT  RANK_CODE_6,RANK_DESC_6,RANK_CODE_5 ,RANK_DESC_5 ,RANK_CODE_4 ,RANK_DESC_4 ,
RANK_CODE_3 ,RANK_DESC_3 ,RANK_CODE_2 ,RANK_DESC_2 ,RANK_CODE_1 , RANK_DESC_1
FROM WORK.WIDE WHERE RANK6_CODE IS NOT NULL;
/*Manually changed the order of retrieval through PROC SQL,Is there any way to do this in a different way?*/
/*I'm getting a record with NULL as RANK6_CODE with this query,why is that?*/
RUN;
QUIT;

PROC DATASETS NOLIST NODETAILS;
CONTENTS DATA=PROJECT.REQ1 OUT=WORK.details;
RUN;

PROC PRINT DATA=PROJECT.REQ1;
RUN;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know if you have any questions. I'm sure i must have made some mistakes here as I'm a beginner. So please point it out so that i can learn. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 14:05:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598613#M172647</guid>
      <dc:creator>Scott177</dc:creator>
      <dc:date>2019-10-23T14:05:43Z</dc:date>
    </item>
    <item>
      <title>Re: Need to transpose data with multiple variables in a better way</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598620#M172648</link>
      <description>&lt;P&gt;Post your data as self-contained data steps, using datalines, that we can cut-and-paste into SAS.&amp;nbsp; Use Insert SAS Code (running man icon) to do so.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your mangled cut-and-paste of your input and output data makes it hard for us to help you.&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 06:40:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598620#M172648</guid>
      <dc:creator>ScottBass</dc:creator>
      <dc:date>2019-10-23T06:40:05Z</dc:date>
    </item>
    <item>
      <title>Re: Need to transpose data with multiple variables in a better way</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598644#M172652</link>
      <description>Thanks ScottBass for the suggestion. I have modified the code, please let me know if i missed anything</description>
      <pubDate>Wed, 23 Oct 2019 09:28:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598644#M172652</guid>
      <dc:creator>Scott177</dc:creator>
      <dc:date>2019-10-23T09:28:42Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically sort the column variables in the desired order without PROC SQL manipulation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598721#M172689</link>
      <description>&lt;P&gt;The code you posted doesn't match your data, but I think I know what you were trying to accomplish.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your actual code was correct, then the following might be what you're seeking.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The reason you got the blank record is because you didn't include an NWAY option on your proc summary statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used the macro you can copy and paste from&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/A-better-way-to-FLIP-i-e-transpose-make-wide-a-dataset/ta-p/433620" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/A-better-way-to-FLIP-i-e-transpose-make-wide-a-dataset/ta-p/433620&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I used it because running it replaces everything in your code except for the initial datastep. I added one line to that datastep in order to only keep the numbers from you rank_key field.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code I ran:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;libname project '/folders/myfolders';
DATA PROJECT.IMPORT;
  INPUT RANK6_CODE RANK_CODE $ RANK_DESC $ RANK_KEY $;
  rank_key=compress(rank_key, , 'kd');
  DATALINES;
1 467 Sample_Description0 Rank_1
1 120 Sample_Description1 Rank_2
1 456 Sample_Description2 Rank_3
1 778 Sample_Description3 Rank_4
1 780 Sample_Description4 Rank_5
1 402 Sample_Description5 Rank_6
3 567 Sample_Description0 Rank_1
3 220 Sample_Description1 Rank_2
3 556 Sample_Description2 Rank_3
3 878 Sample_Description3 Rank_4
3 880 Sample_Description4 Rank_5
3 502 Sample_Description5 Rank_6
;
RUN;

%transpose(data=PROJECT.IMPORT, out=PROJECT.outputfile,
  by=rank6_code, id=rank_key,sort=yes, descendingid=yes,
  delimiter=_, autovars=all)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 Oct 2019 14:19:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Dynamically-sort-the-column-variables-in-the-desired-order/m-p/598721#M172689</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2019-10-23T14:19:17Z</dc:date>
    </item>
  </channel>
</rss>

