<?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: Arrange dataset variable in group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275216#M55011</link>
    <description>&lt;PRE&gt;
I love this question. Why the output couldn't be :

ID	English	Biology	Chemistry
101	Eng1	 	 
101	Eng2	Bio1	 
102	Eng2	Bio2	Che2
102	Eng1	Bio1	Che1
103	Eng1	Bio2	 
103	Eng3	Bio1	 




data have;
infile cards truncover;
input ID     (Name    Group1     Group1_ID1   Group1_D2   Group2     Group2_ID1   Group2_D2  Group3    Group3_ID1   Group3_D2) (: $20.);
cards;   
101   Mike     English      Eng2               Eng1             Biology     Bio1                         
102   Julia     English      Eng1               Eng2             Biology     Bio1               Bio2              Chemistry Che1             Che2             
103   Luc      English      Eng3                Eng1             Biology     Bio1              Bio2 
;
run;

proc transpose data=have out=temp;
by id;
var group1--Group3_D2;
run;
data data(index=(x=(id g2)) drop=_name_) group_name(keep=_name_ col1);
 set temp;
 length g1 g2 $ 40;
 g1=upcase(scan(_name_,1,'_'));
 g2=upcase(scan(_name_,2,'_'));
 if not missing(g2) then output data;
 if missing(g2) and not missing(col1) then output group_name;
run;


proc sort data=group_name nodupkey;by _name_ col1;run;
proc sql noprint;
 select cats(_name_,'=',col1) into : rename separated by ' '
  from group_name;
quit;
%put &amp;amp;rename;


proc sql noprint;
 select distinct catt('data(where=(g1="',g1,'") rename=(col1=',g1,'))') 
  into : merge separated by ' '
   from data;
quit;
%put &amp;amp;merge;
data want(rename=(&amp;amp;rename));
 merge &amp;amp;merge;
 by id g2;
 drop g1 g2;
run;


&lt;/PRE&gt;</description>
    <pubDate>Sun, 05 Jun 2016 04:13:00 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-06-05T04:13:00Z</dc:date>
    <item>
      <title>Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275209#M55007</link>
      <description>&lt;DIV&gt;Hi All,&lt;/DIV&gt;
&lt;DIV&gt;I never had a dataset like this:&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv7289917607yui_3_16_0_ym19_1_1465077346676_2862"&gt;ID &amp;nbsp; &amp;nbsp; Name &amp;nbsp; &amp;nbsp;Group1 &amp;nbsp; &amp;nbsp; Group1_ID1 &amp;nbsp; &lt;SPAN&gt;Group1_D2 &amp;nbsp; Group2 &amp;nbsp; &amp;nbsp; Group2_ID1 &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;Group2_D2 &amp;nbsp;Group3 &amp;nbsp; &amp;nbsp;Group3_ID1 &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;Group3_D2&lt;/SPAN&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV id="yiv7289917607yui_3_16_0_ym19_1_1465077346676_2863"&gt;101 &amp;nbsp; Mike &amp;nbsp; &amp;nbsp; &lt;SPAN&gt;English&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;Eng2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Eng1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;Biology &amp;nbsp; &amp;nbsp; Bio1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;102 &amp;nbsp; Julia&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;English&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Eng1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Eng2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;Biology &amp;nbsp; &amp;nbsp; Bio1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Bio2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Chemistry&amp;nbsp;Che1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Che2&amp;nbsp; &amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;103 &amp;nbsp; Luc &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;English&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Eng3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Eng1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;Biology &amp;nbsp; &amp;nbsp; Bio1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Bio2&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;I always worked with dataset like the following. Can someone tell me the code how to convert the above dataset like below. My original dataset has 35 groups, so I need a code that is efficient.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;ID &amp;nbsp; &amp;nbsp; English &amp;nbsp;Biology &amp;nbsp; Chemistry&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;101 &amp;nbsp;Eng2 &amp;nbsp; &amp;nbsp; &amp;nbsp;Bio1 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;101 &amp;nbsp;Eng1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Che2&amp;nbsp;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;102 &amp;nbsp;Eng1 &amp;nbsp; &amp;nbsp; &amp;n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SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV id="yiv7289917607yui_3_16_0_ym19_1_1465077346676_2864"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sun, 05 Jun 2016 03:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275209#M55007</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-06-05T03:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275213#M55008</link>
      <description>&lt;P&gt;Please try the transpose approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards missover;
input ID     Name$   Group1$     Group1_ID1$   Group1_D2$   Group2$     Group2_ID1$   Group2_D2$  Group3$    Group3_ID1$   Group3_D2$;
cards;
101   Mike     English      Eng2               Eng1             Biology     Bio1                         
102   Julia     English      Eng1               Eng2             Biology     Bio1               Bio2              Chemistry Che1             Che2             
103   Luc      English      Eng3                Eng1             Biology     Bio1              Bio2 
;

proc transpose data=have out=trans1;
by id;
id group1 ;
var  Group1_ID1 Group1_D2 ;
run;

proc transpose data=have out=trans2;
by id;
id group2 ;
var  Group2_ID1 Group2_D2 ;
run;

proc transpose data=have out=trans3;
by id;
id group3 ;
var  Group3_ID1 Group3_D2 ;
run;

data all(drop=_name_);
merge trans1 trans2 trans3;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jun 2016 03:37:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275213#M55008</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-06-05T03:37:48Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275214#M55009</link>
      <description>Thanks Jaga. I have total 35 group. So transposing 35 times is not efficient. Can you please wtite a code that is more efficient please.</description>
      <pubDate>Sun, 05 Jun 2016 03:52:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275214#M55009</guid>
      <dc:creator>mlogan</dc:creator>
      <dc:date>2016-06-05T03:52:11Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275216#M55011</link>
      <description>&lt;PRE&gt;
I love this question. Why the output couldn't be :

ID	English	Biology	Chemistry
101	Eng1	 	 
101	Eng2	Bio1	 
102	Eng2	Bio2	Che2
102	Eng1	Bio1	Che1
103	Eng1	Bio2	 
103	Eng3	Bio1	 




data have;
infile cards truncover;
input ID     (Name    Group1     Group1_ID1   Group1_D2   Group2     Group2_ID1   Group2_D2  Group3    Group3_ID1   Group3_D2) (: $20.);
cards;   
101   Mike     English      Eng2               Eng1             Biology     Bio1                         
102   Julia     English      Eng1               Eng2             Biology     Bio1               Bio2              Chemistry Che1             Che2             
103   Luc      English      Eng3                Eng1             Biology     Bio1              Bio2 
;
run;

proc transpose data=have out=temp;
by id;
var group1--Group3_D2;
run;
data data(index=(x=(id g2)) drop=_name_) group_name(keep=_name_ col1);
 set temp;
 length g1 g2 $ 40;
 g1=upcase(scan(_name_,1,'_'));
 g2=upcase(scan(_name_,2,'_'));
 if not missing(g2) then output data;
 if missing(g2) and not missing(col1) then output group_name;
run;


proc sort data=group_name nodupkey;by _name_ col1;run;
proc sql noprint;
 select cats(_name_,'=',col1) into : rename separated by ' '
  from group_name;
quit;
%put &amp;amp;rename;


proc sql noprint;
 select distinct catt('data(where=(g1="',g1,'") rename=(col1=',g1,'))') 
  into : merge separated by ' '
   from data;
quit;
%put &amp;amp;merge;
data want(rename=(&amp;amp;rename));
 merge &amp;amp;merge;
 by id g2;
 drop g1 g2;
run;


&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jun 2016 04:13:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275216#M55011</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-05T04:13:00Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275217#M55012</link>
      <description>&lt;PRE&gt;
Here is :
Just change 
var group-- 
into
var group:  ;
for referring to variables.







data have;
infile cards truncover;
input ID     (Name    Group1     Group1_ID1   Group1_D2   Group2     Group2_ID1   Group2_D2  Group3    Group3_ID1   Group3_D2) (: $20.);
cards;   
101   Mike     English      Eng2               Eng1             Biology     Bio1                         
102   Julia     English      Eng1               Eng2             Biology     Bio1               Bio2              Chemistry Che1             Che2             
103   Luc      English      Eng3                Eng1             Biology     Bio1              Bio2 
;
run;

proc transpose data=have out=temp;
by id;
var group: ;
run;
data data(index=(x=(id g2)) drop=_name_) group_name(keep=_name_ col1);
 set temp;
 length g1 g2 $ 40;
 g1=upcase(scan(_name_,1,'_'));
 g2=upcase(scan(_name_,2,'_'));
 if not missing(g2) then output data;
 if missing(g2) and not missing(col1) then output group_name;
run;


proc sort data=group_name nodupkey;by _name_ col1;run;
proc sql noprint;
 select cats(_name_,'=',col1) into : rename separated by ' '
  from group_name;
quit;
%put &amp;amp;rename;


proc sql noprint;
 select distinct catt('data(where=(g1="',g1,'") rename=(col1=',g1,'))') 
  into : merge separated by ' '
   from data;
quit;
%put &amp;amp;merge;
data want(rename=(&amp;amp;rename));
 merge &amp;amp;merge;
 by id g2;
 drop g1 g2;
run;





&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jun 2016 04:16:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275217#M55012</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-06-05T04:16:36Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275225#M55013</link>
      <description>&lt;P&gt;It is a good idea indeed to simplify your data structure. Another transposition approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
length Group1 Group1_ID1 Group1_D2 Group2 Group2_ID1 Group2_D2 Group3 Group3_ID1 Group3_D2 $20;
infile cards missover;
input ID Name$ Group1 Group1_ID1 Group1_D2 Group2 Group2_ID1 Group2_D2 Group3 Group3_ID1 Group3_D2;
cards;
101 Mike English Eng2 Eng1 Biology Bio1 
102 Julia English Eng1 Eng2 Biology Bio1 Bio2 Chemistry Che1 Che2 
103 Luc English Eng3 Eng1 Biology Bio1 Bio2 
;

data list0;
length gName gExt gValue $20;
set have;
array g group:;
do i = 1 to dim(g);
    if not missing(g{i}) then do;
        gName = scan(vname(g{i}), 1, "_");        
        gExt = scan(vname(g{i}), 2, "_");
        gValue = g{i};
        output;
        end;
    end;
keep id gName gExt gValue;
run;

proc sort data=list0; by id gName gExt; run;

data list1;
do until(last.gName);
    set list0; by id gName;
    if first.gName then var = gValue;
    else output;
    end;
run;

proc sort data=list1; by id gExt;

proc transpose data=list1 out=want(drop=_name_);
by id gExt;
var gValue;
id var;
run;

proc print data=want noobs; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 05 Jun 2016 04:48:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275225#M55013</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-06-05T04:48:43Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275227#M55014</link>
      <description>&lt;P&gt;we could automate with do loop and call execute as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;option mprint;
%macro test(i);
proc transpose data=have out=trans&amp;amp;i;
where group&amp;amp;i ne '';
by id;
id group&amp;amp;i ;
var  Group&amp;amp;i._ID1 Group&amp;amp;i._D2 ;
run;
%mend;

Data _null_;
do i = 1 to 35;
call execute('%test('||i||')');
end;
run;
&lt;BR /&gt;data all(drop=_name_);&lt;BR /&gt;merge trans: ;&lt;BR /&gt;run;&lt;BR /&gt;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I am not sure if trans: (colon) will work. could you please test it.&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jun 2016 05:28:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275227#M55014</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-06-05T05:28:21Z</dc:date>
    </item>
    <item>
      <title>Re: Arrange dataset variable in group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275232#M55016</link>
      <description>&lt;P&gt;I'd actually suggest you go one step further and include the subject as a variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Subject Level&lt;/P&gt;
&lt;P&gt;1 English Eng1&lt;/P&gt;
&lt;P&gt;1 English Eng2&lt;/P&gt;
&lt;P&gt;1 Chemistry Chem2&lt;/P&gt;</description>
      <pubDate>Sun, 05 Jun 2016 08:34:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Arrange-dataset-variable-in-group/m-p/275232#M55016</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-06-05T08:34:18Z</dc:date>
    </item>
  </channel>
</rss>

