<?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: How to put the variables in alphabetical order in a dataset in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75308#M21836</link>
    <description>HI!&lt;BR /&gt;
The easy way is to put length before set:&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
 length Age Height 8 Name $8 Sex $1 Weight 8;&lt;BR /&gt;
 set sashelp.class;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Works if you know alll variables, else you can create length statment using proc contents and macro.&lt;BR /&gt;
&lt;BR /&gt;
//Fredrik</description>
    <pubDate>Tue, 24 Feb 2009 13:55:06 GMT</pubDate>
    <dc:creator>FredrikE</dc:creator>
    <dc:date>2009-02-24T13:55:06Z</dc:date>
    <item>
      <title>How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75307#M21835</link>
      <description>I have to change the dataset in such a way that all the variables should be in alphabetical order.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
       The help will be appreciated.</description>
      <pubDate>Tue, 24 Feb 2009 11:33:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75307#M21835</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-24T11:33:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75308#M21836</link>
      <description>HI!&lt;BR /&gt;
The easy way is to put length before set:&lt;BR /&gt;
&lt;BR /&gt;
data test;&lt;BR /&gt;
 length Age Height 8 Name $8 Sex $1 Weight 8;&lt;BR /&gt;
 set sashelp.class;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Works if you know alll variables, else you can create length statment using proc contents and macro.&lt;BR /&gt;
&lt;BR /&gt;
//Fredrik</description>
      <pubDate>Tue, 24 Feb 2009 13:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75308#M21836</guid>
      <dc:creator>FredrikE</dc:creator>
      <dc:date>2009-02-24T13:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75309#M21837</link>
      <description>You don't want to use a LENGTH statement because you don't need to know those attributes and you don't want to fiddle them.  Too much trouble.&lt;BR /&gt;
&lt;BR /&gt;
You just need alphabetical list and RETAIN.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
   select name into :vars separated by ' '&lt;BR /&gt;
      from dictionary.columns&lt;BR /&gt;
      where libname eq 'SASHELP' and memname eq 'CLASS'&lt;BR /&gt;
      order by name&lt;BR /&gt;
      ;&lt;BR /&gt;
   quit;&lt;BR /&gt;
   run;&lt;BR /&gt;
%put NOTE: VARS=&amp;amp;vars;&lt;BR /&gt;
&lt;BR /&gt;
data work.class;&lt;BR /&gt;
   retain &amp;amp;vars;&lt;BR /&gt;
   set sashelp.class;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 24 Feb 2009 14:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75309#M21837</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-02-24T14:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75310#M21838</link>
      <description>Your example works if all obs has values on every var, try this:&lt;BR /&gt;
data test;&lt;BR /&gt;
 length b a g d 8;&lt;BR /&gt;
 b = 1; a = 2; d=10; g = 30;output;&lt;BR /&gt;
 b = 2; g = 31;output;&lt;BR /&gt;
 b = 3; a = 9; d=7; g = 32;output;&lt;BR /&gt;
 b = 4; g = 33;output;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data test2; &lt;BR /&gt;
 retain a b d g;&lt;BR /&gt;
 set test;&lt;BR /&gt;
run;&lt;BR /&gt;
proc print data = test2;run;&lt;BR /&gt;
&lt;BR /&gt;
retain is used for retaining vars....I think...:-)&lt;BR /&gt;
&lt;BR /&gt;
I made a small macro that i hope works , even with work-tables...;-)&lt;BR /&gt;
&lt;BR /&gt;
%macro sortvar(ds);&lt;BR /&gt;
  proc contents data = &amp;amp;ds out = metadata;run;&lt;BR /&gt;
  proc sort data = metadata; by name;run;&lt;BR /&gt;
&lt;BR /&gt;
  data _null_;&lt;BR /&gt;
    retain counter 0;&lt;BR /&gt;
    set metadata end=last;&lt;BR /&gt;
      counter+1;&lt;BR /&gt;
      call symput('VAR'||compress(put(counter,8.)),trim(name));&lt;BR /&gt;
      if type eq 1 then call symput('TYP'||compress(put(counter,8.)),trim(''));&lt;BR /&gt;
      else call symput('TYP'||compress(put(counter,8.)),trim('$'));&lt;BR /&gt;
      call symput('LEN'||compress(put(counter,8.)),trim(length));&lt;BR /&gt;
    if last then do;&lt;BR /&gt;
      call symput('NOVARS',compress(put(counter,8.)));&lt;BR /&gt;
 end;&lt;BR /&gt;
  run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data &amp;amp;ds;&lt;BR /&gt;
  %DO I = 1 %TO &amp;amp;NOVARS;&lt;BR /&gt;
    length &amp;amp;&amp;amp;VAR&amp;amp;I &amp;amp;&amp;amp;TYP&amp;amp;I&amp;amp;&amp;amp;LEN&amp;amp;I;&lt;BR /&gt;
  %END;&lt;BR /&gt;
 set &amp;amp;ds;&lt;BR /&gt;
run;&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
/* example of usage, replace sashelp.class with your own dataset */&lt;BR /&gt;
%sortvar(sashelp.prdsale);&lt;BR /&gt;
&lt;BR /&gt;
//Fredrik</description>
      <pubDate>Tue, 24 Feb 2009 14:30:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75310#M21838</guid>
      <dc:creator>FredrikE</dc:creator>
      <dc:date>2009-02-24T14:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75311#M21839</link>
      <description>&amp;gt; Your example works if all obs has values on every&lt;BR /&gt;
&amp;gt; var, try this:&lt;BR /&gt;
&amp;gt; data test;&lt;BR /&gt;
&amp;gt;  length b a g d 8;&lt;BR /&gt;
&amp;gt; b = 1; a = 2; d=10; g = 30;output;&lt;BR /&gt;
&amp;gt;  b = 2; g = 31;output;&lt;BR /&gt;
&amp;gt; b = 3; a = 9; d=7; g = 32;output;&lt;BR /&gt;
&amp;gt;  b = 4; g = 33;output;&lt;BR /&gt;
&amp;gt; un;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; data test2; &lt;BR /&gt;
&amp;gt;  retain a b d g;&lt;BR /&gt;
&amp;gt; set test;&lt;BR /&gt;
&amp;gt; run;&lt;BR /&gt;
&amp;gt; proc print data = test2;run;&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; retain is used for retaining vars....I think...:-)&lt;BR /&gt;
&lt;BR /&gt;
This is a common misconception.&lt;BR /&gt;
&lt;BR /&gt;
Your example is flawed but I have corrected it below to show that using RETAIN is "safe" for reordering variables read with a SET statement.  RETAIN is for retaining values but RETAIN does not have any effect on the values of variables read using a SET statement other than to reorder them when RETAIN is placed before the SET statement.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;&lt;BR /&gt;
   length b a g d 8;&lt;BR /&gt;
   b = 1; a = 2; d=10; g = 30;output;&lt;BR /&gt;
   call missing(of _all_);&lt;BR /&gt;
   b = 2; g = 31; output;&lt;BR /&gt;
   call missing(of _all_);&lt;BR /&gt;
   b = 3; a = 9; d=7; g = 32;output;&lt;BR /&gt;
   call missing(of _all_);&lt;BR /&gt;
   b = 4; g = 33; output;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
data test2; &lt;BR /&gt;
   retain a b d g;&lt;BR /&gt;
   set test;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
**Output from second PROC PRINT;&lt;BR /&gt;
&lt;I&gt;Obs    a    b     d     g&lt;BR /&gt;
&lt;BR /&gt;
 1     2    1    10    30&lt;BR /&gt;
 2     .    2     .    31&lt;BR /&gt;
 3     9    3     7    32&lt;BR /&gt;
 4     .    4     .    33&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 24 Feb 2009 14:58:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75311#M21839</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-02-24T14:58:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75312#M21840</link>
      <description>Saw my misstake, always like that with quick fixes and tests.....great, it workes fine!&lt;BR /&gt;
Thanks!&lt;BR /&gt;
Nice macro thou'.....isn't it...:)</description>
      <pubDate>Tue, 24 Feb 2009 15:18:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75312#M21840</guid>
      <dc:creator>FredrikE</dc:creator>
      <dc:date>2009-02-24T15:18:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75313#M21841</link>
      <description>&amp;gt; Nice macro thou'.....isn't it...:)&lt;BR /&gt;
Well you don't need a macro to do this and if you did I would not create all those macro variables.  &lt;BR /&gt;
&lt;BR /&gt;
What your example does remind me of is the need for PROC CONTENTS as it provides alphabetical order by default, (you didn't need to sort), with special "attention" to SAS enumerated variable lists. (a1 followed by a2 not a10).  This ordering is not obtained by a simple sort of the NAMES as in my first example using DICTIONARY.COLUMNS.&lt;BR /&gt;
&lt;BR /&gt;
See new example.&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data test;&lt;BR /&gt;
   length b a g d 8 a4-a11 a1-a3 8;&lt;BR /&gt;
   b = 1; a = 2; d=10; g = 30;output;&lt;BR /&gt;
   call missing(of _all_);&lt;BR /&gt;
   b = 2; g = 31; output;&lt;BR /&gt;
   call missing(of _all_);&lt;BR /&gt;
   b = 3; a = 9; d=7; g = 32;output;&lt;BR /&gt;
   call missing(of _all_);&lt;BR /&gt;
   b = 4; g = 33; output;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc print;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc contents data=work.test out=work.names(keep=name);&lt;BR /&gt;
   run;&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
   select name into :vars seperated by ' '&lt;BR /&gt;
      from work.names&lt;BR /&gt;
      ;&lt;BR /&gt;
   quit;&lt;BR /&gt;
   run;&lt;BR /&gt;
%put NOTE: vars=&amp;amp;vars;&lt;BR /&gt;
&lt;BR /&gt;
data test2; &lt;BR /&gt;
   retain &amp;amp;vars;&lt;BR /&gt;
   set test;&lt;BR /&gt;
   run;&lt;BR /&gt;
proc contents varnum;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;I&gt; Variables in Creation Order&lt;BR /&gt;
&lt;BR /&gt;
 #    Variable    Type    Len&lt;BR /&gt;
&lt;BR /&gt;
 1    a           Num       8&lt;BR /&gt;
 2    a1          Num       8&lt;BR /&gt;
 3    a2          Num       8&lt;BR /&gt;
 4    a3          Num       8&lt;BR /&gt;
 5    a4          Num       8&lt;BR /&gt;
 6    a5          Num       8&lt;BR /&gt;
 7    a6          Num       8&lt;BR /&gt;
 8    a7          Num       8&lt;BR /&gt;
 9    a8          Num       8&lt;BR /&gt;
10    a9          Num       8&lt;BR /&gt;
11    a10         Num       8&lt;BR /&gt;
12    a11         Num       8&lt;BR /&gt;
13    b           Num       8&lt;BR /&gt;
14    d           Num       8&lt;BR /&gt;
15    g           Num       8&lt;/I&gt;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Tue, 24 Feb 2009 15:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75313#M21841</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2009-02-24T15:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75314#M21842</link>
      <description>Here's another solution that's pretty easy:&lt;BR /&gt;
&lt;BR /&gt;
/* Create a dataset containing a list of variable names */&lt;BR /&gt;
   proc contents data=adam.adsl out=contents (keep=name);&lt;BR /&gt;
   run;&lt;BR /&gt;
/* Sort the list by variable name */&lt;BR /&gt;
   proc sort data=contents;&lt;BR /&gt;
      by name;&lt;BR /&gt;
   run;&lt;BR /&gt;
/* Create a macro variable containing a string of variable names */&lt;BR /&gt;
   data _null_;&lt;BR /&gt;
      set contents end=eof;&lt;BR /&gt;
      length lst $200.;&lt;BR /&gt;
      retain lst;&lt;BR /&gt;
      lst = strip(lst) || " " || strip(name);&lt;BR /&gt;
      if eof then&lt;BR /&gt;
         call symput('lst',lst);&lt;BR /&gt;
   run;&lt;BR /&gt;
/* Reference the macro variable in a retain statement */&lt;BR /&gt;
   data adsl_x;&lt;BR /&gt;
      retain &amp;amp;lst;&lt;BR /&gt;
      set adam.adsl;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
The only problem that might occur is if you have lots of variables and the list can't fit into a single string - but it's easy enough to create more than one string if necessary</description>
      <pubDate>Wed, 25 Feb 2009 16:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75314#M21842</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-02-25T16:37:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75315#M21843</link>
      <description>it worked</description>
      <pubDate>Thu, 26 Feb 2009 05:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75315#M21843</guid>
      <dc:creator>R_Win</dc:creator>
      <dc:date>2009-02-26T05:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75316#M21844</link>
      <description>THis worked for me, but I drop off the last 8 columns. Any advice?</description>
      <pubDate>Thu, 23 Jul 2009 23:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75316#M21844</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-07-23T23:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to put the variables in alphabetical order in a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75317#M21845</link>
      <description>And yet another solution:&lt;BR /&gt;
&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
select name into :vars separated by ','&lt;BR /&gt;
from dictionary.columns&lt;BR /&gt;
where libname eq 'SASHELP' and memname eq 'CLASS'&lt;BR /&gt;
order by name; &lt;BR /&gt;
create table WORK.CLASS as&lt;BR /&gt;
select &amp;amp;VARS from SASHELP.CLASS;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;.</description>
      <pubDate>Mon, 27 Jul 2009 07:45:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-put-the-variables-in-alphabetical-order-in-a-dataset/m-p/75317#M21845</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-07-27T07:45:23Z</dc:date>
    </item>
  </channel>
</rss>

