<?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: Order variables in dataset in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73823#M21419</link>
    <description>Thanks, guys. Already the first proc sort option did it. I will check the others as well.</description>
    <pubDate>Wed, 22 Sep 2010 17:11:40 GMT</pubDate>
    <dc:creator>blackraven</dc:creator>
    <dc:date>2010-09-22T17:11:40Z</dc:date>
    <item>
      <title>Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73817#M21413</link>
      <description>Hi.&lt;BR /&gt;
&lt;BR /&gt;
I would like to order variables in alphabetic/numeric order, e.g. a1, a1a, a2, a10a, a11.&lt;BR /&gt;
&lt;BR /&gt;
If I use proc contents (keep = name), then proc sql to put the variables into a macro variable and then use retain in a data procedure, it will not put the above variables in correct order.&lt;BR /&gt;
&lt;BR /&gt;
Could it be solved in another way?</description>
      <pubDate>Wed, 22 Sep 2010 10:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73817#M21413</guid>
      <dc:creator>blackraven</dc:creator>
      <dc:date>2010-09-22T10:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73818#M21414</link>
      <description>Will you get what you want if you first sort the results of your proc contents?  Depending upon the order you want, you may need 9.2 and have to use:&lt;BR /&gt;
SORTSEQ=linguistic(NUMERIC_COLLATION=ON) when using proc sort.&lt;BR /&gt;
&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Wed, 22 Sep 2010 11:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73818#M21414</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2010-09-22T11:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73819#M21415</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
At first, you should be careful with retain as there could be some variables with missing values and if you use retain you could get unexpected values. &lt;BR /&gt;
&lt;BR /&gt;
Actually retain should put variables in correct order, only then you should put this retain before set statement. Another way – you could use format statement instead:&lt;BR /&gt;
&lt;BR /&gt;
data one;&lt;BR /&gt;
b=1;&lt;BR /&gt;
a=2;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data two;&lt;BR /&gt;
format a b;&lt;BR /&gt;
set one;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
But then you should manually define variables and their formats if necessary. I think there should be some more elegant method, but if you don‘t have too many variables, this one could work, only you have to do that manually.&lt;BR /&gt;
&lt;BR /&gt;
Ieva</description>
      <pubDate>Wed, 22 Sep 2010 11:56:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73819#M21415</guid>
      <dc:creator>ieva</dc:creator>
      <dc:date>2010-09-22T11:56:34Z</dc:date>
    </item>
    <item>
      <title>Re: Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73820#M21416</link>
      <description>Hi,&lt;BR /&gt;
&lt;BR /&gt;
Use Proc contents to get the list of variables, then use below sql code, which creates new SAS table called sample_t from exitsing sample table, with variables in the order u mentioned in select statement &lt;BR /&gt;
&lt;BR /&gt;
proc sql ; &lt;BR /&gt;
create table sample_t as &lt;BR /&gt;
select a1, a2, a3, a4,........a10, a11&lt;BR /&gt;
from sample;&lt;BR /&gt;
quit;</description>
      <pubDate>Wed, 22 Sep 2010 12:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73820#M21416</guid>
      <dc:creator>samHT</dc:creator>
      <dc:date>2010-09-22T12:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73821#M21417</link>
      <description>Hi&lt;BR /&gt;
&lt;BR /&gt;
I got lately sick of typing especially in SQL select clauses with same named variables from different tables (like match keys). &lt;BR /&gt;
&lt;BR /&gt;
I therefore created a macro which does all this typing for me.&lt;BR /&gt;
&lt;BR /&gt;
Hope it will be of some use for you.&lt;BR /&gt;
&lt;BR /&gt;
options nomprint nomlogic nosymbolgen;&lt;BR /&gt;
&lt;BR /&gt;
%macro AttrList(LibAndTable,KeepList=,DropList=);&lt;BR /&gt;
&lt;BR /&gt;
  %global SAS_DSAttrList SAS_SQLColumnList;&lt;BR /&gt;
  %local KeepListComma DropListComma;&lt;BR /&gt;
&lt;BR /&gt;
  %let LibAndTable=%upcase(&amp;amp;LibAndTable);&lt;BR /&gt;
  %let KeepList=%cmpres(%upcase(&amp;amp;KeepList));&lt;BR /&gt;
  %let DropList=%cmpres(%upcase(&amp;amp;DropList));&lt;BR /&gt;
&lt;BR /&gt;
  %let KeepListComma=%sysfunc(prxchange(%str(s/\b +\b/','/oi), -1, &amp;amp;KeepList));&lt;BR /&gt;
  %if %bquote(&amp;amp;KeepListComma) ne %bquote() %then %let KeepListComma=%str(%')&amp;amp;KeepListComma%str(%');&lt;BR /&gt;
 &lt;BR /&gt;
  %let DropListComma=%sysfunc(prxchange(%str(s/\b +\b/','/oi), -1, &amp;amp;DropList));&lt;BR /&gt;
  %if %bquote(&amp;amp;DropListComma) ne %bquote() %then %let DropListComma=%str(%')&amp;amp;DropListComma%str(%');&lt;BR /&gt;
&lt;BR /&gt;
  %put DropListComma= &amp;amp;DropListComma;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
  proc datasets lib=work nolist nowarn memtype=(data view);&lt;BR /&gt;
    delete __VarAndAttrListTBL;&lt;BR /&gt;
  quit;&lt;BR /&gt;
&lt;BR /&gt;
  proc sql noprint;&lt;BR /&gt;
&lt;BR /&gt;
    /* create work table __VarAndAttrListTBL containing variable names and attributes */ &lt;BR /&gt;
    create table __VarAndAttrListTBL as&lt;BR /&gt;
      select &lt;BR /&gt;
            varnum&lt;BR /&gt;
          ,name&lt;BR /&gt;
          ,type &lt;BR /&gt;
          ,length&lt;BR /&gt;
          ,format&lt;BR /&gt;
          ,informat&lt;BR /&gt;
          ,label&lt;BR /&gt;
&lt;BR /&gt;
      from dictionary.columns&lt;BR /&gt;
&lt;BR /&gt;
      where   libname="%scan(WORK.&amp;amp;LibAndTable,-2,'.')" &lt;BR /&gt;
          and memname="%scan(&amp;amp;LibAndTable,-1,'.')"&lt;BR /&gt;
          %if %bquote(&amp;amp;KeepList) ne %bquote() %then&lt;BR /&gt;
          %do;&lt;BR /&gt;
            and upcase(name) in (%unquote(&amp;amp;KeepListComma))&lt;BR /&gt;
          %end;&lt;BR /&gt;
          %if %bquote(&amp;amp;DropList) ne %bquote() %then&lt;BR /&gt;
          %do;&lt;BR /&gt;
            and upcase(name) not in (%unquote(&amp;amp;DropListComma))&lt;BR /&gt;
          %end;&lt;BR /&gt;
&lt;BR /&gt;
/*      order by varnum*/&lt;BR /&gt;
      order by name&lt;BR /&gt;
    ;&lt;BR /&gt;
&lt;BR /&gt;
    /* create macro var &amp;amp;SAS_DSAttrList containing list of vars like needed for Data step Attrib statement */ &lt;BR /&gt;
    select &lt;BR /&gt;
      catx(' ',&lt;BR /&gt;
          name&lt;BR /&gt;
          ,case upcase(type)&lt;BR /&gt;
            when 'NUM' then cats('length=',length)&lt;BR /&gt;
            else cats('length=$',length)&lt;BR /&gt;
            end&lt;BR /&gt;
          ,case&lt;BR /&gt;
            when format ne '' then cats('format=',format) &lt;BR /&gt;
            else ''&lt;BR /&gt;
            end &lt;BR /&gt;
          ,case &lt;BR /&gt;
            when informat ne '' then cats('informat=',informat)&lt;BR /&gt;
            else ''&lt;BR /&gt;
            end&lt;BR /&gt;
          ,case &lt;BR /&gt;
            when label ne '' then cats("label='",label,"'")&lt;BR /&gt;
            else ''&lt;BR /&gt;
            end&lt;BR /&gt;
        )&lt;BR /&gt;
        into :SAS_DSAttrList separated by ' '&lt;BR /&gt;
&lt;BR /&gt;
      from __VarAndAttrListTBL&lt;BR /&gt;
/*      order by varnum*/&lt;BR /&gt;
      order by name&lt;BR /&gt;
    ;&lt;BR /&gt;
&lt;BR /&gt;
    /* create macro var &amp;amp;SAS_SQLSelectList containing list of vars like needed for SAS SQL Select clause */ &lt;BR /&gt;
    select &lt;BR /&gt;
      catx(' ',&lt;BR /&gt;
          name&lt;BR /&gt;
          ,cats('length=',length)&lt;BR /&gt;
          ,case&lt;BR /&gt;
            when format ne '' then cats('format=',format) &lt;BR /&gt;
            else ''&lt;BR /&gt;
            end &lt;BR /&gt;
          ,case &lt;BR /&gt;
            when informat ne '' then cats('informat=',informat)&lt;BR /&gt;
            else ''&lt;BR /&gt;
            end&lt;BR /&gt;
          ,case &lt;BR /&gt;
            when label ne '' then cats("label='",label,"'")&lt;BR /&gt;
            else ''&lt;BR /&gt;
            end&lt;BR /&gt;
        )&lt;BR /&gt;
        into :SAS_SQLColumnList separated by ' ,'&lt;BR /&gt;
&lt;BR /&gt;
      from __VarAndAttrListTBL&lt;BR /&gt;
/*      order by varnum*/&lt;BR /&gt;
      order by name&lt;BR /&gt;
    ;&lt;BR /&gt;
&lt;BR /&gt;
  quit;&lt;BR /&gt;
&lt;BR /&gt;
  %put SAS_DSAttrList    =  &amp;amp;SAS_DSAttrList;&lt;BR /&gt;
  %put SAS_SQLColumnList =  &amp;amp;SAS_SQLColumnList;&lt;BR /&gt;
&lt;BR /&gt;
%mend;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data have;&lt;BR /&gt;
  a2=1;&lt;BR /&gt;
  a1=1;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
%AttrList(work.have)&lt;BR /&gt;
&lt;BR /&gt;
data want;&lt;BR /&gt;
  attrib &amp;amp;SAS_DSAttrList;&lt;BR /&gt;
  set have;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
proc contents data=want;&lt;BR /&gt;
quit;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
Cheers&lt;BR /&gt;
Patrick&lt;BR /&gt;
&lt;BR /&gt;
Had to change 'order by varnum' to 'order by name' to cover for this requirement&lt;BR /&gt;
&lt;BR /&gt;
    &lt;BR /&gt;
Message was edited by: Patrick</description>
      <pubDate>Wed, 22 Sep 2010 13:02:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73821#M21417</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2010-09-22T13:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73822#M21418</link>
      <description>...&lt;BR /&gt;
&amp;gt; I would like to order variables in alphabetic/numeric&lt;BR /&gt;
&amp;gt; order, e.g. a1, a1a, a2, a10a, a11.&lt;BR /&gt;
&amp;gt; &lt;BR /&gt;
&amp;gt; If I use proc contents (keep = name), then proc sql&lt;BR /&gt;
&amp;gt; to put the variables into a macro variable and then&lt;BR /&gt;
&amp;gt; use retain in a data procedure, it will not put the&lt;BR /&gt;
&amp;gt; above variables in correct order.&lt;BR /&gt;
...&lt;BR /&gt;
This sounds like a reasonable approach. Below surely is. IMHO, that is. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
[pre]&lt;BR /&gt;
   /* test data */&lt;BR /&gt;
   data one;&lt;BR /&gt;
     retain a1 a10a a11 a1a a2 .;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   /* prepare an ordered list of vars */&lt;BR /&gt;
   %let num = 0123456789;&lt;BR /&gt;
   %let ordered=;&lt;BR /&gt;
&lt;BR /&gt;
   proc sql noprint;&lt;BR /&gt;
     select   name into :ordered separated by " "&lt;BR /&gt;
     from     dictionary.columns&lt;BR /&gt;
     where    libname="WORK" and memname="ONE"&lt;BR /&gt;
     order by scan(name, 1, "&amp;amp;num") &lt;BR /&gt;
            , inputn(scan(name, 1, "&amp;amp;num", "k"), "best")&lt;BR /&gt;
            , scan(name, 2, "&amp;amp;num"); &lt;BR /&gt;
   quit;&lt;BR /&gt;
&lt;BR /&gt;
   /* re-order */&lt;BR /&gt;
   data two;&lt;BR /&gt;
     retain &amp;amp;ordered;&lt;BR /&gt;
     set one;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
   /* check */&lt;BR /&gt;
   proc contents data=two order=varnum;&lt;BR /&gt;
   run;&lt;BR /&gt;
   /* on lst -- in part&lt;BR /&gt;
   #    Variable    Type    Len&lt;BR /&gt;
   1    a1          Num       8&lt;BR /&gt;
   2    a1a         Num       8&lt;BR /&gt;
   3    a2          Num       8&lt;BR /&gt;
   4    a10a        Num       8&lt;BR /&gt;
   5    a11         Num       8&lt;BR /&gt;
   */&lt;BR /&gt;
[/pre]</description>
      <pubDate>Wed, 22 Sep 2010 14:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73822#M21418</guid>
      <dc:creator>chang_y_chung_hotmail_com</dc:creator>
      <dc:date>2010-09-22T14:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73823#M21419</link>
      <description>Thanks, guys. Already the first proc sort option did it. I will check the others as well.</description>
      <pubDate>Wed, 22 Sep 2010 17:11:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73823#M21419</guid>
      <dc:creator>blackraven</dc:creator>
      <dc:date>2010-09-22T17:11:40Z</dc:date>
    </item>
    <item>
      <title>Re: Order variables in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73824#M21420</link>
      <description>Patrick's macro and SQL solution can be modified to create the same order as PROC SORT with NUMERIC_COLLATION=ON by using the SORTKEY function:&lt;BR /&gt;
&lt;BR /&gt;
  order by sortkey(name,'','','','N')</description>
      <pubDate>Wed, 22 Sep 2010 19:21:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Order-variables-in-dataset/m-p/73824#M21420</guid>
      <dc:creator>scmebu</dc:creator>
      <dc:date>2010-09-22T19:21:42Z</dc:date>
    </item>
  </channel>
</rss>

