<?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 all variables vertically in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955464#M373148</link>
    <description>&lt;P&gt;In general, your question to sort all variables doesn't make sense. Once you sort by variable V1, this may make V2 out of sort order, and if you sort by V2, this may make V1 out of sort order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, when &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; asks "What is your intended result?", I also want to know in words what you mean by "sort all variables". Showing us for this extremely small data set may not be sufficient. (And by the way &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt; you should always explain in detail what you want, and give examples of the desired output, in your first post, without us having to ask).&lt;/P&gt;</description>
    <pubDate>Wed, 08 Jan 2025 11:07:22 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2025-01-08T11:07:22Z</dc:date>
    <item>
      <title>sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955448#M373141</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data v_sort;
input v1$ v2$ V3$ ;
cards;
A  B  C
F  K  J
D  E  F
C  K  J
K  L  W
S  V  R
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;how to sort all variables&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2025 05:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955448#M373141</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2025-01-08T05:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955459#M373146</link>
      <description>What is your intended result?</description>
      <pubDate>Wed, 08 Jan 2025 10:11:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955459#M373146</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-01-08T10:11:32Z</dc:date>
    </item>
    <item>
      <title>Re: sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955464#M373148</link>
      <description>&lt;P&gt;In general, your question to sort all variables doesn't make sense. Once you sort by variable V1, this may make V2 out of sort order, and if you sort by V2, this may make V1 out of sort order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, when &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; asks "What is your intended result?", I also want to know in words what you mean by "sort all variables". Showing us for this extremely small data set may not be sufficient. (And by the way &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt; you should always explain in detail what you want, and give examples of the desired output, in your first post, without us having to ask).&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2025 11:07:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955464#M373148</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-08T11:07:22Z</dc:date>
    </item>
    <item>
      <title>Re: sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955465#M373149</link>
      <description>&lt;P&gt;Looks like a school homework task to me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data v_sort;
input v1$ v2$ V3$ ;
cards;
A  B  C
F  K  J
D  E  F
C  K  J
K  L  W
S  V  R
;
run;

proc contents data=v_sort out=list(keep=NAME varnum) noprint;
proc sort data=list;
by varnum;
run;


data _null_;
set list end=_E_;
call execute(cats('proc sort data=v_sort(keep=',name,') out=___',varnum,'; by _all_; run;'));
if _E_ then call execute(cats('data v_sort2; merge ___1-___',varnum,'; run;'));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2025 11:11:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955465#M373149</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-01-08T11:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955642#M373205</link>
      <description>&lt;P&gt;I assume that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;'s intepretation of what you want is correct:&amp;nbsp; i.e. that each of the columns V1, V2, and V3 should be independently sorted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a single-pass of the data that does what you request:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set v_sort  end=end_of_data;
  array cols {*} v1-v3;
  if _n_=1 then do;
    declare hash _v1 (ordered:'a');
      _v1.definekey('v1','_n_');
      _v1.definedata('v1');
      _v1.definedone();
    declare hiter _i1 ('_v1');
    declare hash _v2 (ordered:'a');
      _v2.definekey('v2','_n_');
      _v2.definedata('v2');
      _v2.definedone();
    declare hiter _i2 ('_v2');
    declare hash _v3 (ordered:'a');
      _v3.definekey('v3','_n_');
      _v3.definedata('v3');
      _v3.definedone();
    declare hiter _i3 ('_v3');
  end;

  _v1.add();
  _v2.add();
  _v3.add();
  if end_of_data;
  do while(_i1.next()=0 and _i2.next()=0 and _i3.next()=0);
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The hash keys above include _N_ as a secondary key, to simplify treatment of ties in the primary key.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, this program becomes a lot bigger as the number of columns to be sorted increases.&amp;nbsp; Below is a program whose size is constant regardless of the number of columns to be sorted.&amp;nbsp; But note that, while the columns can be put into an array of increasing size, the hash objects can't be put into an array.&amp;nbsp; Instead, a hash-of-hashes performs that service:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (keep=v:);
  set v_sort end=end_of_data;
  array cols{*} v1-v3;
  if _n_=1 then do;
    col=.;
    declare hash h ;  /*Reserve "H" as a hash-object name, not as a varname*/
    declare hiter hi ;
    declare hash hoh ();
      hoh.definekey ('col');
      hoh.definedata('h','hi');
      hoh.definedone();

    do col=1 to dim(cols);
      h=_new_ hash (ordered:'a');  /*"Instantiate" an instance of the H hash object */
        h.definekey(vname(cols{col}),'_n_');
        h.definedata(vname(cols{col}));
        h.definedone();
      hi=_new_ hiter('h');
      hoh.add();
    end;
  end;

  do col=1 to dim(cols);
    hoh.find();  /*Load the column-specific hash*/
    h.add();
  end;

  if end_of_data then do i=1 to _n_;
    do col=1 to dim(cols);
      hoh.find();
      hi.next();
    end;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 May 2025 19:57:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955642#M373205</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-05-17T19:57:15Z</dc:date>
    </item>
    <item>
      <title>Re: sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955650#M373206</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I assume that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;'s intepretation of what you want is correct:&amp;nbsp; i.e. that each of the columns V1, V2, and V3 should be independently sorted.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I did not assume this, it seems as if Kurt did not assume this, and while you may be correct that this is what the OP wants, you may also be incorrect. Again, I request&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;provide more explanation about what he wants. We shouldn't have to guess, we shouldn't have to assume, and none of us wants to spend time coming up with a solution that is not what the OP wants.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2025 16:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955650#M373206</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-09T16:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955651#M373207</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I assume that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;'s intepretation of what you want is correct:&amp;nbsp; i.e. that each of the columns V1, V2, and V3 should be independently sorted.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I did not assume this, it seems as if Kurt did not assume this, and while you may be correct that this is what the OP wants, you may also be incorrect. Again, I request&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;provide more explanation about what he wants. We shouldn't have to guess, we shouldn't have to assume.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I agree in that request.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jan 2025 16:47:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/955651#M373207</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2025-01-09T16:47:18Z</dc:date>
    </item>
    <item>
      <title>Re: sort all variables vertically</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/966642#M376190</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;does this answer your question? (Please omit the &lt;EM&gt;proc sql;&lt;/EM&gt; step, that step was just to see if the v1,v2,v3 tables generated correctly, you do not need the &lt;EM&gt;proc sql;&lt;/EM&gt; step to generate the final output)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data v_sort;
   input v1$ v2$ v3$;
   cards;
A B C
F K J
D E F
C K J
K L W
S V R
;
run;
data v1(keep=v1)
     v2(keep=v2)
     v3(keep=v3);
   set v_sort;
run;
proc sql noprint;
select * from v1;
select * from v2;
select * from v3;
quit;
proc sort data=v1;by v1;run;
proc sort data=v2;by v2;run;
proc sort data=v3;by v3;run;
data final;
   merge v1 v2 v3;
run;
proc print data=final;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1747335046100.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106973i21CD4D65A4D727C7/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1747335046100.png" alt="dxiao2017_0-1747335046100.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The repeated &lt;EM&gt;data;set;&lt;/EM&gt; and &lt;EM&gt;proc sort;&lt;/EM&gt; steps can be replaced by a macro, like this (the code, log, and result are as follows): (please omit the &lt;EM&gt;proc sql;&lt;/EM&gt; step, that step was just to see if the tables v1,v2,v3 generated correctly, you do not need the proc sql; step for the final table)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data v_sort;
   input v1$ v2$ v3$;
   cards;
A B C
F K J
D E F
C K J
K L W
S V R
;
run;
%macro sortvars;
%do i=1 %to 3;
data v&amp;amp;i(keep=v&amp;amp;i);
   set v_sort;
run;
proc sql noprint;
select * from v&amp;amp;i;
quit;
proc sort data=v&amp;amp;i;by v&amp;amp;i;run;
%end;
%mend sortvars;
options symbolgen mlogic mprint;
%sortvars;
options nosymbolgen nomlogic nomprint;
data final;
   merge v1 v2 v3;
run;
proc print data=final;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt; 69         data v_sort;
 70            input v1$ v2$ v3$;
 71            cards;
 
 NOTE: The data set WORK.V_SORT has 6 observations and 3 variables.&lt;/PRE&gt;
&lt;PRE&gt; 78         ;
 79         run;
 80         %macro sortvars;
 81         %do i=1 %to 3;
 82         data v&amp;amp;i(keep=v&amp;amp;i);
 83            set v_sort;
 84         run;
 85         proc sql noprint;
 86         select * from v&amp;amp;i;
 87         quit;
 88         proc sort data=v&amp;amp;i;by v&amp;amp;i;run;
 89         %end;
 90         %mend sortvars;
 91         options symbolgen mlogic mprint;
 92         %sortvars;
 MLOGIC(SORTVARS):  Beginning execution.
 MLOGIC(SORTVARS):  %DO loop beginning; index variable I; start value is 1; stop value is 3; &lt;BR /&gt;by value is 1.  
 SYMBOLGEN:  Macro variable I resolves to 1
 SYMBOLGEN:  Macro variable I resolves to 1
 MPRINT(SORTVARS):   data v1(keep=v1);
 MPRINT(SORTVARS):   set v_sort;
 MPRINT(SORTVARS):   run;
 
 NOTE: There were 6 observations read from the data set WORK.V_SORT.
 NOTE: The data set WORK.V1 has 6 observations and 1 variables.&lt;/PRE&gt;
&lt;PRE&gt; MPRINT(SORTVARS):   proc sql noprint;
 SYMBOLGEN:  Macro variable I resolves to 1
 MPRINT(SORTVARS):   select * from v1;
 MPRINT(SORTVARS):   quit;
 NOTE: PROCEDURE SQL used (Total process time):&lt;/PRE&gt;
&lt;PRE&gt; SYMBOLGEN:  Macro variable I resolves to 1
 MPRINT(SORTVARS):   proc sort data=v1;
 SYMBOLGEN:  Macro variable I resolves to 1
 MPRINT(SORTVARS):  by v1;
 MPRINT(SORTVARS):  run;
 
 NOTE: There were 6 observations read from the data set WORK.V1.
 NOTE: The data set WORK.V1 has 6 observations and 1 variables.&lt;/PRE&gt;
&lt;PRE&gt; MLOGIC(SORTVARS):  %DO loop index variable I is now 2; loop will iterate again.
 SYMBOLGEN:  Macro variable I resolves to 2
 SYMBOLGEN:  Macro variable I resolves to 2
 MPRINT(SORTVARS):   data v2(keep=v2);
 MPRINT(SORTVARS):   set v_sort;
 MPRINT(SORTVARS):   run;
 
 NOTE: There were 6 observations read from the data set WORK.V_SORT.&lt;BR /&gt;&lt;SPAN&gt; NOTE: The data set WORK.V2 has 6 observations and 1 variables.&lt;/SPAN&gt;&lt;/PRE&gt;
&lt;PRE&gt; MPRINT(SORTVARS):   proc sql noprint;
 SYMBOLGEN:  Macro variable I resolves to 2
 MPRINT(SORTVARS):   select * from v2;
 MPRINT(SORTVARS):   quit;
 NOTE: PROCEDURE SQL used (Total process time):&lt;/PRE&gt;
&lt;PRE&gt; SYMBOLGEN:  Macro variable I resolves to 2
 MPRINT(SORTVARS):   proc sort data=v2;
 SYMBOLGEN:  Macro variable I resolves to 2
 MPRINT(SORTVARS):  by v2;
 MPRINT(SORTVARS):  run;
 
 NOTE: There were 6 observations read from the data set WORK.V2.
 NOTE: The data set WORK.V2 has 6 observations and 1 variables.&lt;/PRE&gt;
&lt;PRE&gt; MLOGIC(SORTVARS):  %DO loop index variable I is now 3; loop will iterate again.
 SYMBOLGEN:  Macro variable I resolves to 3
 SYMBOLGEN:  Macro variable I resolves to 3
 MPRINT(SORTVARS):   data v3(keep=v3);
 MPRINT(SORTVARS):   set v_sort;
 MPRINT(SORTVARS):   run;
 
 NOTE: There were 6 observations read from the data set WORK.V_SORT.
 NOTE: The data set WORK.V3 has 6 observations and 1 variables.&lt;/PRE&gt;
&lt;PRE&gt; MPRINT(SORTVARS):   proc sql noprint;
 SYMBOLGEN:  Macro variable I resolves to 3
 MPRINT(SORTVARS):   select * from v3;
 MPRINT(SORTVARS):   quit;&lt;/PRE&gt;
&lt;PRE&gt; SYMBOLGEN:  Macro variable I resolves to 3
 MPRINT(SORTVARS):   proc sort data=v3;
 SYMBOLGEN:  Macro variable I resolves to 3
 MPRINT(SORTVARS):  by v3;
 MPRINT(SORTVARS):  run;
 
 NOTE: There were 6 observations read from the data set WORK.V3.
 NOTE: The data set WORK.V3 has 6 observations and 1 variables.&lt;/PRE&gt;
&lt;PRE&gt; MLOGIC(SORTVARS):  %DO loop index variable I is now 4; loop will not iterate again.
 MLOGIC(SORTVARS):  Ending execution.
 93         options nosymbolgen nomlogic nomprint;
 94         data final;
 95            merge v1 v2 v3;
 96         run;
 
 NOTE: There were 6 observations read from the data set WORK.V1.
 NOTE: There were 6 observations read from the data set WORK.V2.
 NOTE: There were 6 observations read from the data set WORK.V3.
 NOTE: The data set WORK.FINAL has 6 observations and 3 variables.&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1747335665684.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106974iAC4B518BE160AB6F/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_1-1747335665684.png" alt="dxiao2017_1-1747335665684.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 May 2025 19:16:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sort-all-variables-vertically/m-p/966642#M376190</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-05-15T19:16:52Z</dc:date>
    </item>
  </channel>
</rss>

