<?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: Sorting variables by type in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337887#M76810</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select name into :varlist separated by ' '
from dictionary.columns
where libname='WORK'
and memname='YOURTBL'
order by type, name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Add the name column to the order by statement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 03 Mar 2017 16:18:57 GMT</pubDate>
    <dc:creator>nehalsanghvi</dc:creator>
    <dc:date>2017-03-03T16:18:57Z</dc:date>
    <item>
      <title>Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337829#M76786</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am wondering if there is any way to sort variables by type? For example, all numeric variables are appear first, and then comes categorical variables. The only thing I think that MAY work is using proc sql, but I am not too familiar with that. Any help or suggestions would be greatly appreciated. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 14:27:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337829#M76786</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-03-03T14:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337830#M76787</link>
      <description>&lt;P&gt;there is a way, but to what purpose? &amp;nbsp;In most SAS procedures you can use syntax to run on all numeric (or character) variables, regardless of how they are ordered. &amp;nbsp;What is the ultimate problem that you are trying to solve?&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 14:29:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337830#M76787</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-03-03T14:29:49Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337832#M76788</link>
      <description>Where do you want them sorted (by sorted, I am assuming you mean arranged)? In a certain dataset?</description>
      <pubDate>Fri, 03 Mar 2017 14:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337832#M76788</guid>
      <dc:creator>nehalsanghvi</dc:creator>
      <dc:date>2017-03-03T14:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337836#M76790</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;I am using arrays to help clean very large datasets, and from what I know, arrays in SAS can only hold variables of the same type. The macro we have right now requires us to "alternate" once there's a change in variable type. So, we're looking to see if it's at all possible to group variables of the same type together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/41810"&gt;@nehalsanghvi&lt;/a&gt;&amp;nbsp;Yup, just arranged by type.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 14:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337836#M76790</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-03-03T14:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337838#M76791</link>
      <description>&lt;P&gt;Take a look at metadata available in dictionary.columns. You can use this to pick up only the columns of a single datatype:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select name into :numvarlist separated by ' '
from dictionary.columns
where libname='WORK'
and memname='YOURTBL'
and type='num';
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Mar 2017 14:40:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337838#M76791</guid>
      <dc:creator>nehalsanghvi</dc:creator>
      <dc:date>2017-03-03T14:40:03Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337839#M76792</link>
      <description>I will try this once I head to work in a bit. Will let you know how it goes--thank you so much.</description>
      <pubDate>Fri, 03 Mar 2017 14:43:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337839#M76792</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-03-03T14:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337841#M76794</link>
      <description>&lt;P&gt;I suggest you look into &lt;A href="http://support.sas.com/documentation/cdl/en/lestmtsref/69738/HTML/default/viewer.htm#p08do6szetrxe2n136ush727sbuo.htm" target="_self"&gt;using the _NUMERIC_ and _CHARACTER_ keywords when you define the arrays.&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;For example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array NumerVars[*] _NUMERIC_;
array CharVars[*] _CHARACTER_;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Mar 2017 14:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337841#M76794</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-03-03T14:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337843#M76795</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; The macros we have take a "range" of consecutive variables (e.g., firstvar--lastvar). Not sure if I understand how to use your logic here.</description>
      <pubDate>Fri, 03 Mar 2017 14:49:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337843#M76795</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-03-03T14:49:42Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337846#M76796</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Rearranging the order of the PDV putting numerics before character variables

inspired by
https://goo.gl/B3yGv9
https://communities.sas.com/t5/Base-SAS-Programming/Sorting-variables-by-type/m-p/337829


HAVE
====

Up to 40 obs from sashelp.class total obs=19

Obs    NAME       SEX    AGE    HEIGHT    WEIGHT

  1    Alfred      M      14     69.0      112.5
  2    Alice       F      13     56.5       84.0
  3    Barbara     F      13     65.3       98.0
  4    Carol       F      14     62.8      102.5
  5    Henry       M      14     63.5      102.5
  6    James       M      12     57.3       83.0
...

WANT
====

Up to 40 obs from want total obs=19

Obs    AGE    HEIGHT    WEIGHT    NAME       SEX

  1     14     69.0      112.5    Alfred      M
  2     13     56.5       84.0    Alice       F
  3     13     65.3       98.0    Barbara     F
  4     14     62.8      102.5    Carol       F
  5     14     63.5      102.5    Henry       M
  6     12     57.3       83.0    James       M

WORKING CODE
============

    DOSUBL
      array num _numeric_;
      chrs=catx(' ',chrs,vname(chr[i]));
      call symputx("retnum",nums);

      retain &amp;amp;retnum;

FULL SOLUTION
=============

%symdel retnum; * just in case it exists;
data want;

 * get meta data;
 if _n_ =0 then do;

   %let rc=%sysfunc(dosubl('

    data _null_;
      set sashelp.class(obs=1);
      array num _numeric_;
      length nums $4096;
      do i=1 to dim(num);
         nums=catx(' ',nums,vname(num[i]));
      end;
      call symputx("retnum",nums);
    run;quit;

      '));
  end;

    retain &amp;amp;retnum;
    set sashelp.class;

  run;quit;

run;quit;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Mar 2017 14:55:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337846#M76796</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-03T14:55:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337849#M76798</link>
      <description>&lt;P&gt;If I correctly understand what you are trying to do, then the following would work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  select name into :reorder separated by ' '
    from dictionary.columns
      where libname=upcase('sashelp') and
            memname=upcase('class')
        order by type descending
  ;
quit;

data want;
  retain &amp;amp;reorder.;
  set sashelp.class;
run;

&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 15:03:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337849#M76798</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-03-03T15:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337852#M76800</link>
      <description>&lt;P&gt;A basic principle in efficient data analysis is to not rearrange the data unless necessary. If you say more about what sort of "data cleaning" you are interested in, we can provide specific suggestions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, if you want to drop observations for which any variable is missing and also want to drop observations for which any numerical variable has a negative value, you can say:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Want;
set sashelp.Heart;
array NumerVars[*] _NUMERIC_;
array CharVars[*] _CHARACTER_;
if ^CMISS(of NumerVars[*], of CharVars[*]) &amp;amp;  /* omit any missing */
   min(of NumerVars[*]) &amp;gt; 0;                  /* omit any negative values */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 15:04:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337852#M76800</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2017-03-03T15:04:54Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337860#M76802</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/122635"&gt;@corkee&lt;/a&gt; wrote:&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt; The macros we have take a "range" of consecutive variables (e.g., firstvar--lastvar). Not sure if I understand how to use your logic here.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You should provide some more details. Arrays can be defined with lists such that when the variables have the same stem you just indicate either the start and end: Thisvar1 - ThisVar10 or ThisVar:&amp;nbsp; with the : indicating to assign all variables&amp;nbsp;whose names start with ThisVar to the array. Neither of these require the variables to be in any specific order/location in the data vector. If your macro is not using the array -- range construct (array somearray thisvar -- anothervarname;) explicitly then you may be misunderstanding the requirement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your macro requires adjacent variables by order in the data vector then 1) they are so fragile they need to be rewritten or 2) the concept behind the data structure needs to be addressed. What you are describing (need to re order variables in the data vector) seems to indicate that you have an existing data structure that keeps adding variables (at the end) of the data. Which is indicative of a poorly designed approach.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 15:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337860#M76802</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-03T15:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337884#M76809</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;&amp;nbsp;This did the trick. However, is there a way to sort the variables, after sorting them by type, in alphabetical order? Thank you so much.&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 16:16:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337884#M76809</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-03-03T16:16:41Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337887#M76810</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select name into :varlist separated by ' '
from dictionary.columns
where libname='WORK'
and memname='YOURTBL'
order by type, name;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Add the name column to the order by statement&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 03 Mar 2017 16:18:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337887#M76810</guid>
      <dc:creator>nehalsanghvi</dc:creator>
      <dc:date>2017-03-03T16:18:57Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337893#M76812</link>
      <description>Got it. I just played around and eventually figured it out. THANK YOU.</description>
      <pubDate>Fri, 03 Mar 2017 16:24:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337893#M76812</guid>
      <dc:creator>corkee</dc:creator>
      <dc:date>2017-03-03T16:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: Sorting variables by type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337976#M76837</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;This will put the variables in alphabetic order&lt;BR /&gt;&lt;BR /&gt;%symdel odrvars vardim odr; * just in case it exists;
data want;

 * get meta data;
 if _n_ =0 then do;

   %let rc=%sysfunc(dosubl('

    data _null_;
      set sashelp.class(obs=1);
      array num _numeric_;
      array chr _character_;
      length vars $4096;
      do i=1 to dim(num);
         vars=catx(",",vars,quote(vname(num[i])));
      end;
      do i=1 to dim(chr);
         vars=catx(",",vars,quote(vname(chr[i])));
      end;
      call symputx("odrvars",vars);
      call symputx("vardim",put(dim(num)+dim(chr),5.));
    run;quit;
    data _null_;
      length odr $4096;
      array varall[&amp;amp;vardim] $32 (&amp;amp;odrvars);
      call sortc(of varall(*));
      odr=catx(" ",of varall[*]);
      call symputx("odr",odr);
    run;quit;
    '));
  end;

    retain &amp;amp;odr;
    set sashelp.class;

  run;quit;

run;quit;


Up to 40 obs WORK.WANT total obs=19

Obs    AGE    HEIGHT    NAME       SEX    WEIGHT

  1     14     69.0     Alfred      M      112.5
  2     13     56.5     Alice       F       84.0
  3     13     65.3     Barbara     F       98.0
  4     14     62.8     Carol       F      102.5
  5     14     63.5     Henry       M      102.5





&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 03 Mar 2017 19:54:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sorting-variables-by-type/m-p/337976#M76837</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2017-03-03T19:54:47Z</dc:date>
    </item>
  </channel>
</rss>

