<?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 for multiple varaibles in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955279#M373089</link>
    <description>&lt;P&gt;Sorry but this is not the final solution, need one more transpose to get in it full wide structure&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2025 06:05:32 GMT</pubDate>
    <dc:creator>Ronein</dc:creator>
    <dc:date>2025-01-07T06:05:32Z</dc:date>
    <item>
      <title>long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955048#M372991</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;LEt's say I have a data set called Have with Lonf structure.&lt;/P&gt;
&lt;P&gt;I want to convert it to wide structure.&lt;/P&gt;
&lt;P&gt;I know to convery long to side for one varaible using proc transpose.&lt;/P&gt;
&lt;P&gt;My question-&lt;/P&gt;
&lt;P&gt;Here I am doing x6 times proc transpose .&lt;/P&gt;
&lt;P&gt;IS there a beter way to do it with less code that tranpsose Long to&amp;nbsp; wide for 6 varaibles together?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have'
input CustID YYYYMM monIndex  X W Z R Q Y ;
cards;
111 202401 1 10 11 13 15 19 1
111 202402 2 17 17 12 10 18 0
111 202403 3 19 18 12 12 11 0
111 202404 4 12 12 12 13 14 1
111 202405 5 16 15 14 18 17 1
111 202406 6 16 17 17 18 15 0
222 202401 1 11 11 11 11 11 0
222 202402 2 12 13 14 11 16 0
222 202403 3 19 19 18 16 19 1
222 202404 4 17 17 16 15 18 1
222 202405 5 16 15 16 16 17 0
222 202406 6 14 16 14 13 17 0
;
Run
proc transpose data=have out=wide_1;
by CustID ;
ID monIndex  ;
Var X;
Run;

proc transpose data=have out=wide_2;
by CustID ;
ID monIndex  ;
Var W;
Run;

proc transpose data=have out=wide_3;
by CustID ;
ID monIndex  ;
Var Z;
Run;


proc transpose data=have out=wide_4;
by CustID ;
ID monIndex  ;
Var R;
Run;


proc transpose data=have out=wide_5;
by CustID ;
ID monIndex  ;
Var Q;
Run;

proc transpose data=have out=wide_6;
by CustID ;
ID monIndex  ;
Var Y;
Run;

Data want;
Merge wide_1-wide_6;
by CustID;
Run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2025 19:05:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955048#M372991</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-01-03T19:05:45Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955050#M372992</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 19&lt;/A&gt;?&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 19:19:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955050#M372992</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-03T19:19:23Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955064#M372995</link>
      <description>&lt;P&gt;Your WANT dataset is identical to WIDE_6, which presumably is not your intent.&amp;nbsp; Please show what you expect WANT to look like.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 19:49:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955064#M372995</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-01-03T19:49:16Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955070#M372997</link>
      <description>&lt;P&gt;VAR statement accepts multiple variables, no need to transpose each variable separately.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=want prefix=Month_;
by CustID ;
ID monIndex  ;
Var X W Z R Q Y;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Jan 2025 20:00:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955070#M372997</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2025-01-03T20:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955085#M373001</link>
      <description>&lt;P&gt;Since you have multiple variables your starting point is not truly a TALL dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You should first convert your intermediate dataset into a TALL dataset.&amp;nbsp; Then you can transpose that TALL dataset to a WIDE dataset.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=tall;
  by custid yyyymm monIndex;
  var x -- y ;
run;

proc transpose data=tall  out=want(drop=_name_);
  by custid ;
  id _name_ monindex;
  var col1 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1735938826219.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103469i8B63DCDBA460CE19/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1735938826219.png" alt="Tom_0-1735938826219.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;If you want the new variables ordered differently add a PROC SORT step in between.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort;
  by custid _name_ monindex;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_1-1735938858866.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103470iC7EE6605AEB0BB1E/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_1-1735938858866.png" alt="Tom_1-1735938858866.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Jan 2025 21:14:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955085#M373001</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-03T21:14:29Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955102#M373010</link>
      <description>&lt;P&gt;Tom already gave you Double Transpose solution.&lt;/P&gt;
&lt;P&gt;But if you have a very BIG table,could try MERGE skill proposed by me.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID YYYYMM monIndex  X W Z R Q Y ;
cards;
111 202401 1 10 11 13 15 19 1
111 202402 2 17 17 12 10 18 0
111 202403 3 19 18 12 12 11 0
111 202404 4 12 12 12 13 14 1
111 202405 5 16 15 14 18 17 1
111 202406 6 16 17 17 18 15 0
222 202401 1 11 11 11 11 11 0
222 202402 2 12 13 14 11 16 0
222 202403 3 19 19 18 16 19 1
222 202404 4 17 17 16 15 18 1
222 202405 5 16 15 16 16 17 0
222 202406 6 14 16 14 13 17 0
;
proc sql noprint;
select distinct catt('have(where=(monIndex=',monIndex,') rename=(X=X',monIndex,' W=W',monIndex,' Z=Z',monIndex,' R=R',monIndex,' Q=Q',monIndex,' Y=Y',monIndex,'))')
 into :merge separated by ' '
 from have ;
quit;
data want;
merge &amp;amp;merge.;
by CustId;
drop YYYYMM monIndex;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Ksharp_0-1735978965591.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/103476i0C462DCA0CC96641/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1735978965591.png" alt="Ksharp_0-1735978965591.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jan 2025 08:22:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955102#M373010</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-04T08:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955103#M373011</link>
      <description>&lt;P&gt;If you &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;don't have gap in monIndex variable and dataset has been sorted by CustID and monIndex&lt;/STRONG&gt;&lt;/FONT&gt;, Could try proc summary solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID YYYYMM monIndex  X W Z R Q Y ;
cards;
111 202401 1 10 11 13 15 19 1
111 202402 2 17 17 12 10 18 0
111 202403 3 19 18 12 12 11 0
111 202404 4 12 12 12 13 14 1
111 202405 5 16 15 14 18 17 1
111 202406 6 16 17 17 18 15 0
222 202401 1 11 11 11 11 11 0
222 202402 2 12 13 14 11 16 0
222 202403 3 19 19 18 16 19 1
222 202404 4 17 17 16 15 18 1
222 202405 5 16 15 16 16 17 0
222 202406 6 14 16 14 13 17 0
;
proc sql noprint;
select max(n) into :n
 from (select count(*) as n from have group by CustID);
quit;
proc summary data=have nway;
class CustID;
output out=want(drop=_type_ _freq_) idgroup(out[&amp;amp;n] (X W Z R Q Y)=);
run;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Jan 2025 08:38:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955103#M373011</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-04T08:38:35Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955156#M373039</link>
      <description>&lt;P&gt;Very nice solution.&lt;/P&gt;
&lt;P&gt;You wrote that if data set is big then this solution is better than using proc transpose?&lt;/P&gt;
&lt;P&gt;Can you give a general rule of&amp;nbsp; thumb what is a big data set? (How many rows, how many columns )&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;60 million rows and 10 columns is a big data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jan 2025 18:58:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955156#M373039</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-01-05T18:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955157#M373040</link>
      <description>&lt;P&gt;Thanks for all,&lt;/P&gt;
&lt;P&gt;I summarize the provided solutions&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID YYYYMM monIndex  X W Z R Q Y ;
cards;
111 202401 1 10 11 13 15 19 1
111 202402 2 17 17 12 10 18 0
111 202403 3 19 18 12 12 11 0
111 202404 4 12 12 12 13 14 1
111 202405 5 16 15 14 18 17 1
111 202406 6 16 17 17 18 15 0
222 202401 1 11 11 11 11 11 0
222 202402 2 12 13 14 11 16 0
222 202403 3 19 19 18 16 19 1
222 202404 4 17 17 16 15 18 1
222 202405 5 16 15 16 16 17 0
222 202406 6 14 16 14 13 17 0
;
Run


/**Task: Convert Long to Wide*****/

/***WAY1***/
proc transpose data=have out=wide_1 prefix=X;
by CustID ;
ID monIndex;
Var X;
Run;
proc transpose data=have out=wide_2  prefix=W;
by CustID ;
ID monIndex  ;
Var W;
Run;
proc transpose data=have out=wide_3  prefix=Z;
by CustID ;
ID monIndex  ;
Var Z;
Run;
proc transpose data=have out=wide_4  prefix=R;
by CustID ;
ID monIndex  ;
Var R;
Run;
proc transpose data=have out=wide_5  prefix=Q;
by CustID ;
ID monIndex  ;
Var Q;
Run;
proc transpose data=have out=wide_6  prefix=Y;
by CustID ;
ID monIndex  ;
Var Y;
Run;

Data want;
Merge wide_1-wide_6;
by CustID;
DROP _name_;
Run;

/***WAY2-Quick way--VAR statement accepts multiple variables, no need to transpose each variable separately***/
proc transpose data=have out=want prefix=X;
by CustID ;
ID monIndex;
Var X W Z R Q Y;
/*Var X--Y;*/
DROP _name_;
Run;


/***WAY3-Double Transpose solution***/
proc transpose data=have out=Longer;
by custid yyyymm monIndex;
var x -- y ;
run;
proc sort data=Longer;
by custid _name_ monindex;
run;
/*If you want the new variables ordered differently add a PROC SORT step in between*/
proc transpose data=Longer  out=want(drop=_name_);
by custid ;
id _name_ monindex;
var col1 ;
run;


/***WAY4-IF you have very big data set****/
data want;
Merge 
have(where=(monIndex=1) rename=(X=X1 W=W1 Z=Z1 R=R1 Q=Q1 Y=Y1))
have(where=(monIndex=2) rename=(X=X2 W=W2 Z=Z2 R=R2 Q=Q2 Y=Y2)) 
have(where=(monIndex=3) rename=(X=X3 W=W3 Z=Z3 R=R3 Q=Q3 Y=Y3))
have(where=(monIndex=4) rename=(X=X4 W=W4 Z=Z4 R=R4 Q=Q4 Y=Y4)) 
have(where=(monIndex=5) rename=(X=X5 W=W5 Z=Z5 R=R5 Q=Q5 Y=Y5))
have(where=(monIndex=6) rename=(X=X6 W=W6 Z=Z6 R=R6 Q=Q6 Y=Y6))
;
by CustId;
drop YYYYMM monIndex;
Run;



/***WAY4-IF you have very big data set--Using macro var tooo****/
proc sql noprint;
select distinct catt('have(where=(monIndex=',monIndex,') rename=(X=X',monIndex,' W=W',monIndex,' Z=Z',monIndex,' R=R',monIndex,' Q=Q',monIndex,' Y=Y',monIndex,'))')
into :merge separated by ' '
from have ;
quit;
%put &amp;amp;merge;

data want;
merge &amp;amp;merge.;
by CustId;
drop YYYYMM monIndex;
run;

/**Way5-If you don't have gap in monIndex variable and dataset has been sorted by CustID and monIndex, Could try proc summary solution***/
proc sql noprint;
select max(n) into :n
From (select count(*) as n from have group by CustID);
quit;
%put &amp;amp;n;

proc summary data=have nway;
class CustID;
output out=want(drop=_type_ _freq_) idgroup(out[&amp;amp;n] (X W Z R Q Y)=);
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jan 2025 19:01:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955157#M373040</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-01-05T19:01:22Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955158#M373041</link>
      <description>&lt;P&gt;you are right.&lt;/P&gt;
&lt;P&gt;IT is because I forgot add prefix.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data have;
input CustID YYYYMM monIndex  X W Z R Q Y ;
cards;
111 202401 1 10 11 13 15 19 1
111 202402 2 17 17 12 10 18 0
111 202403 3 19 18 12 12 11 0
111 202404 4 12 12 12 13 14 1
111 202405 5 16 15 14 18 17 1
111 202406 6 16 17 17 18 15 0
222 202401 1 11 11 11 11 11 0
222 202402 2 12 13 14 11 16 0
222 202403 3 19 19 18 16 19 1
222 202404 4 17 17 16 15 18 1
222 202405 5 16 15 16 16 17 0
222 202406 6 14 16 14 13 17 0
;
Run


/**Task: Convert Long to Wide*****/

/***WAY1***/
proc transpose data=have out=wide_1 prefix=X;
by CustID ;
ID monIndex;
Var X;
Run;
proc transpose data=have out=wide_2  prefix=W;
by CustID ;
ID monIndex  ;
Var W;
Run;
proc transpose data=have out=wide_3  prefix=Z;
by CustID ;
ID monIndex  ;
Var Z;
Run;
proc transpose data=have out=wide_4  prefix=R;
by CustID ;
ID monIndex  ;
Var R;
Run;
proc transpose data=have out=wide_5  prefix=Q;
by CustID ;
ID monIndex  ;
Var Q;
Run;
proc transpose data=have out=wide_6  prefix=Y;
by CustID ;
ID monIndex  ;
Var Y;
Run;

Data want;
Merge wide_1-wide_6;
by CustID;
DROP _name_;
Run;
 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jan 2025 19:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955158#M373041</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-01-05T19:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955169#M373048</link>
      <description>Yes.&lt;BR /&gt;proc transpose is too slow to sort a big table.&lt;BR /&gt;I think a big table at least have 5 million obs and 100 variables .</description>
      <pubDate>Mon, 06 Jan 2025 01:13:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955169#M373048</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-06T01:13:55Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955170#M373049</link>
      <description>Your WAY2 is not right.&lt;BR /&gt;The result  is different from me and Tom.</description>
      <pubDate>Mon, 06 Jan 2025 01:17:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955170#M373049</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-01-06T01:17:10Z</dc:date>
    </item>
    <item>
      <title>Re: long to wide for multiple varaibles</title>
      <link>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955279#M373089</link>
      <description>&lt;P&gt;Sorry but this is not the final solution, need one more transpose to get in it full wide structure&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2025 06:05:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/long-to-wide-for-multiple-varaibles/m-p/955279#M373089</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-01-07T06:05:32Z</dc:date>
    </item>
  </channel>
</rss>

