<?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: Sort variables within a row / create new variables from column names in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829709#M327819</link>
    <description>&lt;P&gt;Your second output seems wrong.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original variables where A and C , not A and B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (A B C) (:yymmdd.);
  format A B C yymmdd10.;
datalines;                      
2019/01/01  2018/03/13  . 
2017/02/02  .           2017/02/02
2018/11/18  2018/08/01  2021/07/02
;


data expect; 
  input (date1-date3) (:yymmdd.) (type1-type3) (:$100.);
  format date1-date3 yymmdd10.; 
datalines; 
2018/03/13  2019/01/01  .           B    A  . 
2017/2/2    .           .           A/B  .  .
2018/08/01  2018/11/18  2021/07/02  B    A  C
;

data step1;
  row+1;
  set have;
run;

proc transpose data=step1 out=step2 ;
  by row;
  var a b c ;
run;

proc sort data=step2;
  by row col1;
run;

data want;
  set step2;
  by row col1;
  where not missing(col1);
  array date [3];
  array type [3] $100 ;
  retain date: type: ;
  format date: yymmdd10.;
  if first.row then call missing(of index date[*] type[*]);
  index + first.col1;
  date[index]=col1;
  type[index]=catx('/',type[index],_name_);
  if last.row then output;
  keep row date: type:;
run;

proc print data=want;
run;
 
proc compare data=want compare=expect;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;The COMPARE Procedure
Comparison of WORK.WANT with WORK.EXPECT
(Method=EXACT)

Value Comparison Results for Variables

__________________________________________________________
           ||  Base Value           Compare Value
       Obs ||  type1                 type1
 ________  ||  ___________________+  ___________________+
           ||
        2  ||  A/C                   A/B
__________________________________________________________

&lt;/PRE&gt;</description>
    <pubDate>Mon, 22 Aug 2022 18:14:42 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-08-22T18:14:42Z</dc:date>
    <item>
      <title>Sort variables within a row / create new variables from column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829704#M327816</link>
      <description>&lt;P&gt;I have three date columns as shown in the 'have' table below. I would like to sort them and create a type variable that is the name of the date column (see the 'want' table).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In some cases, two dates may be equal in which case, they will only be counted as one date when sorting them, but both names should be included in the 'type' (see row 2 of 'want').&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I came up with a very clunky way to do this with if/then statements, but I am wondering if there is a better way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
   input A :yymmdd10. B :yymmdd10. C :yymmdd10. ; 
   format A B C yymmdd10.;
   infile datalines delimiter=','; 
   datalines;                      
2019/01/01, 2018/03/13, . 
2017/02/02, . , 2017/02/02
2018/11/18, 2018/08/01, 2021/07/02
;
run;


data want; 
	input date1 :yymmdd10. date2 :yymmdd10. date3 :yymmdd10. type1 $ type2 $ type3 $;
	format date1 date2 date3 yymmdd10.; 
	infile datalines delimiter=',';
	datalines; 
2018/03/13, 2019/01/01, ., B, A, . 
2017/2/2, . , . , A/B, . , .
2018/08/01, 2018/11/18, 2021/07/02, B, A, C
;
run;
	&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Aug 2022 17:50:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829704#M327816</guid>
      <dc:creator>kz_</dc:creator>
      <dc:date>2022-08-22T17:50:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sort variables within a row / create new variables from column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829709#M327819</link>
      <description>&lt;P&gt;Your second output seems wrong.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The original variables where A and C , not A and B.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input (A B C) (:yymmdd.);
  format A B C yymmdd10.;
datalines;                      
2019/01/01  2018/03/13  . 
2017/02/02  .           2017/02/02
2018/11/18  2018/08/01  2021/07/02
;


data expect; 
  input (date1-date3) (:yymmdd.) (type1-type3) (:$100.);
  format date1-date3 yymmdd10.; 
datalines; 
2018/03/13  2019/01/01  .           B    A  . 
2017/2/2    .           .           A/B  .  .
2018/08/01  2018/11/18  2021/07/02  B    A  C
;

data step1;
  row+1;
  set have;
run;

proc transpose data=step1 out=step2 ;
  by row;
  var a b c ;
run;

proc sort data=step2;
  by row col1;
run;

data want;
  set step2;
  by row col1;
  where not missing(col1);
  array date [3];
  array type [3] $100 ;
  retain date: type: ;
  format date: yymmdd10.;
  if first.row then call missing(of index date[*] type[*]);
  index + first.col1;
  date[index]=col1;
  type[index]=catx('/',type[index],_name_);
  if last.row then output;
  keep row date: type:;
run;

proc print data=want;
run;
 
proc compare data=want compare=expect;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;The COMPARE Procedure
Comparison of WORK.WANT with WORK.EXPECT
(Method=EXACT)

Value Comparison Results for Variables

__________________________________________________________
           ||  Base Value           Compare Value
       Obs ||  type1                 type1
 ________  ||  ___________________+  ___________________+
           ||
        2  ||  A/C                   A/B
__________________________________________________________

&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Aug 2022 18:14:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829709#M327819</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-08-22T18:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sort variables within a row / create new variables from column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829721#M327828</link>
      <description>&lt;P&gt;Here is a method that is the same but different.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
   infile datalines delimiter=','; 
   id + 1;
   input A :yymmdd10. B :yymmdd10. C :yymmdd10. ; 
   format A B C yymmdd10.;
   datalines;                      
2019/01/01, 2018/03/13, . 
2017/02/02, . , 2017/02/02
2018/11/18, 2018/08/01, 2021/07/02
;
run;
proc print;
   run;
proc transpose data=have out=tall(where=(not missing(col1)));
   by id;
   run;
proc sort;
   by id col1;
   run;
data tall2;
   length type $8;
   do until(last.col1);
      set tall; by id col1;
      date = col1;
      type = catx('/',type,_name_);
      end;
   format date yymmdd10.;
   run;
proc print;
   run;
proc summary data=tall2 nway;
   class id;
   output out=want(drop=_type_) idgroup(out[3](date type)=);
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 450px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/74577i19ABE85DA8AC9788/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Aug 2022 19:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829721#M327828</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2022-08-22T19:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Sort variables within a row / create new variables from column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829774#M327849</link>
      <description>Thank you! That's exactly what I needed.</description>
      <pubDate>Mon, 22 Aug 2022 23:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829774#M327849</guid>
      <dc:creator>kz_</dc:creator>
      <dc:date>2022-08-22T23:13:37Z</dc:date>
    </item>
    <item>
      <title>Re: Sort variables within a row / create new variables from column names</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829775#M327850</link>
      <description>Thank you! This works too.</description>
      <pubDate>Mon, 22 Aug 2022 23:13:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sort-variables-within-a-row-create-new-variables-from-column/m-p/829775#M327850</guid>
      <dc:creator>kz_</dc:creator>
      <dc:date>2022-08-22T23:13:58Z</dc:date>
    </item>
  </channel>
</rss>

