<?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 in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966773#M376226</link>
    <description>&lt;P&gt;Are your real variable values for tbl sequentially numbered with common name? If not do you want the actual text of the "tbl" to be the name of the resulting variable? And if that is the case have you verified than all of the values of "tbl" are valid variable names prior to this step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so this may be closer to what you want: (Basically the same as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; but without a step that forces the numeric naming).&lt;/P&gt;
&lt;P&gt;This assumes that the values of "field" are grouped by the actual "tbl", as in the likely output from requesting the names of all the tables and variables from a library from the dictionary tables.&amp;nbsp; Note that depending on the setting of VALIDMEMNAME and VALIDVARNAME when that library was created and this code is submitted you might have table names that are not acceptable as variable names. &lt;/P&gt;
&lt;PRE&gt;proc transpose data=have out=trans1
   ;
  by tbl notsorted;
  var field;
run;

proc transpose data=trans1 out=want (drop=_name_)
     ;
  by _name_;
  var col: ;
  id tbl;
run;&lt;/PRE&gt;</description>
    <pubDate>Sun, 18 May 2025 18:51:49 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2025-05-18T18:51:49Z</dc:date>
    <item>
      <title>Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966769#M376222</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the way to create data set want from data set have via proc transpose(or other way)?&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input tbl $ field $;
cards;
tbl1 x
tbl1 y
tbl1 z
tbl2 w
tbl2 x
tbl2 v
tbl2 y
;
run;

data want;
input tbl1 $ tbl2 $ ;
cards;
x w
y x
z v
. y
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Sun, 18 May 2025 04:44:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966769#M376222</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2025-05-18T04:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966770#M376223</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input tbl $ field $;
cards;
tbl1 x
tbl1 y
tbl1 z
tbl2 w
tbl2 x
tbl2 v
tbl2 y
;
run;
proc sort data=have;
by tbl;
run;
proc freq data=have order=data noprint;
by tbl;
table field/out=temp;
run;
proc transpose data=temp out=temp2;
by tbl;
var field;
run;
proc transpose data=temp2 out=want(drop=_:);
var col:;
id tbl;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 May 2025 06:03:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966770#M376223</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-05-18T06:03:53Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966773#M376226</link>
      <description>&lt;P&gt;Are your real variable values for tbl sequentially numbered with common name? If not do you want the actual text of the "tbl" to be the name of the resulting variable? And if that is the case have you verified than all of the values of "tbl" are valid variable names prior to this step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so this may be closer to what you want: (Basically the same as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt; but without a step that forces the numeric naming).&lt;/P&gt;
&lt;P&gt;This assumes that the values of "field" are grouped by the actual "tbl", as in the likely output from requesting the names of all the tables and variables from a library from the dictionary tables.&amp;nbsp; Note that depending on the setting of VALIDMEMNAME and VALIDVARNAME when that library was created and this code is submitted you might have table names that are not acceptable as variable names. &lt;/P&gt;
&lt;PRE&gt;proc transpose data=have out=trans1
   ;
  by tbl notsorted;
  var field;
run;

proc transpose data=trans1 out=want (drop=_name_)
     ;
  by _name_;
  var col: ;
  id tbl;
run;&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 May 2025 18:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966773#M376226</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-05-18T18:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966774#M376227</link>
      <description>&lt;P&gt;I would skip the transpose steps and just do my own code generation.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;filename code temp;
data _null_;
  set have end=eof;
  by tbl;
  file code;
  if _n_=1 then put 'merge';
  if first.tbl then put
   '  have(rename=(field=' tbl ') where=(' tbl =:$quote. '))'
  ;
  if eof then put ';' ;
run;

data want;
%include code / source2;
  drop tbl ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is the step that is generated for this example:&lt;/P&gt;
&lt;PRE&gt;13   data want;
14   %include code / source2;
NOTE: %INCLUDE (level 1) file CODE is (system-specific pathname).
15  +merge
16  +  have(rename=(field=tbl1 ) where=(tbl="tbl1" ))
17  +  have(rename=(field=tbl2 ) where=(tbl="tbl2" ))
18  +;
NOTE: %INCLUDE (level 1) ending.
19     drop tbl ;
20   run;

NOTE: The data set WORK.WANT has 4 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds
&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 May 2025 19:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966774#M376227</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-18T19:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966775#M376228</link>
      <description>&lt;P&gt;If you wanted to use PROC TRANSPOSE you would need to add the missing row identifier that uniquely identifies how to pair the values for field for TBL1 with the values of field for TBL2.&amp;nbsp; And then reorder the data by this row identifier.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
  do row=1 by 1 until(last.tbl);
    set have;
    by tbl;
    output;
  end;
run;

proc sort data=step1;
  by row tbl ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    row    tbl     field

 1      1     tbl1      x
 2      1     tbl2      w
 3      2     tbl1      y
 4      2     tbl2      x
 5      3     tbl1      z
 6      3     tbl2      v
 7      4     tbl2      y
&lt;/PRE&gt;
&lt;P&gt;Now you can transpose it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=step1 out=want(drop=_name_);
  by row;
  id tbl;
  var field;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get&lt;/P&gt;
&lt;PRE&gt;OBS    row    tbl1    tbl2

 1      1      x       w
 2      2      y       x
 3      3      z       v
 4      4              y
&lt;/PRE&gt;</description>
      <pubDate>Sun, 18 May 2025 19:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966775#M376228</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-18T19:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966781#M376229</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;
&lt;P&gt;No&amp;nbsp; need to make a SEQ variable, proc transpose would take care of it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input tbl $ field $;
cards;
tbl1 x
tbl1 y
tbl1 z
tbl2 w
tbl2 x
tbl2 v
tbl2 y
;
run;

proc transpose data=have out=temp2;
by tbl;
var field;
run;
proc transpose data=temp2 out=want(drop=_:);
var col:;
id tbl;
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-1747617596624.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/107023i3E01D87882A1F937/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Ksharp_0-1747617596624.png" alt="Ksharp_0-1747617596624.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 May 2025 01:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966781#M376229</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-05-19T01:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966782#M376230</link>
      <description>&lt;P&gt;Does it work when there are more than 10?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does the fact that the COL variables will be named COL1 ... COL9 COL10 COL11 (and so will sort into COL1 COL10 COL11 COL2 COL3 ... ) matter?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 19 May 2025 01:24:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966782#M376230</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-05-19T01:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966783#M376231</link>
      <description>&lt;P&gt;Tom,&lt;/P&gt;
&lt;P&gt;I don't understand what you mean.&lt;/P&gt;
&lt;P&gt;COLn has nothing to do with sorting 'COL1', 'COL2', ......&lt;/P&gt;
&lt;P&gt;Here my last proc transpose is unlike ballardw 's code . I did not use "BY _NAME_;".&lt;/P&gt;
&lt;P&gt;Check this:&lt;/P&gt;
&lt;PRE&gt;data have;
input tbl $ field $;
cards;
tbl1 c
tbl1 b
tbl1 b
tbl1 a
tbl1 a
tbl1 a
tbl2 w
tbl2 x
tbl2 d
tbl2 a
tbl2 w
tbl2 x
tbl2 d
tbl2 a
tbl2 w
tbl2 x
tbl2 d
tbl2 a
tbl2 w
tbl2 x
tbl2 d
tbl2 a
;
run;

proc transpose data=have out=temp2;
by tbl;
var field;
run;
&lt;STRONG&gt;proc transpose data=temp2 out=want(drop=_:);
var col:;
id tbl;
run;&lt;/STRONG&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 May 2025 01:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966783#M376231</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-05-19T01:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966893#M376236</link>
      <description>&lt;P&gt;This is for the data you posted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
length tbl1 tbl2 $8;
call missing(of tbl:);
merge
  have (where=(tbl="tbl1") rename=(field=tbl1))
  have (where=(tbl="tbl2") rename=(field=tbl2))
;
drop tbl;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For more values of tbl, run a preliminary step to get the list of values, and use macro %DO loops to create the code dynamically:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro want;
%local tables tbl i;

proc sql noprint;
select distinct tbl into :tables separated by " "
from have;
quit;
data want;
length
&amp;amp;tables
$8
;
call missing(of tbl:);
merge
%do i = 1 %to %sysfunc(countw(&amp;amp;tables));
  %let tbl = %scan(&amp;amp;tables,&amp;amp;i);
  have (where=(tbl="&amp;amp;tbl.") rename=(field=&amp;amp;tbl.))
%end;
;
drop tbl;
run;

%mend;

%want&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 19 May 2025 08:50:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/966893#M376236</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-05-19T08:50:27Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967071#M376275</link>
      <description>&lt;P&gt;If you already know the fixed values of variable TBL, then a merge of subsets of HAVE (one subset per TBL value), each with a rename, makes this into a single pass DATA step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input tbl $ field $;
cards;
tbl1 x
tbl1 y
tbl1 z
tbl2 w
tbl2 x
tbl2 v
tbl2 y
run;

data want;
  merge have (where=(tbl='tbl1')  rename=(field=tlb1))
        have (where=(tbl='tbl2')  rename=(field=tlb2)) ;
  drop tbl;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 00:52:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967071#M376275</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-05-21T00:52:18Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967079#M376279</link>
      <description>I can't see that you can do this without losing data consistency. It seems now that you put values on the same row based on the input data order.&lt;BR /&gt;What is the meaning of each row in the new table?</description>
      <pubDate>Wed, 21 May 2025 07:57:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967079#M376279</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2025-05-21T07:57:35Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967096#M376285</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13674"&gt;@LinusH&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I can't see that you can do this without losing data consistency. It seems now that you put values on the same row based on the input data order.&lt;BR /&gt;What is the meaning of each row in the new table?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Good point. It is hard to make sense of the new table, without additional explanation.&lt;/P&gt;</description>
      <pubDate>Wed, 21 May 2025 11:30:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967096#M376285</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-05-21T11:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Long to wide</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967097#M376286</link>
      <description>&lt;P&gt;Here is another possible way to do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input tbl $ field $;
cards;
tbl1 x
tbl1 y
tbl1 z
tbl2 w
tbl2 x
tbl2 v
tbl2 y
;
run;

data have;
set have;
by tbl;
if first.tbl then flag=0;
flag+1;
run;

proc sort data=have;
by flag tbl;
run;

proc transpose data=have out=want(drop=flag _name_);
by flag;
id tbl;
var field;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 May 2025 11:59:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Long-to-wide/m-p/967097#M376286</guid>
      <dc:creator>Kathryn_SAS</dc:creator>
      <dc:date>2025-05-21T11:59:17Z</dc:date>
    </item>
  </channel>
</rss>

