<?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 variable names in a sas dataset ? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/sorting-variable-names-in-a-sas-dataset/m-p/35659#M7056</link>
    <description>Here is some PROC SQL code I frequently use to generate a sorted variable list of periods (PDyymmdd -- output from PROC TRANSPOSE) as columns, output as a SAS macro variable.  Do consider that there is a max length for a macro variable, which may limit use of this technique:&lt;BR /&gt;
&lt;BR /&gt;
data TEST_DATA;&lt;BR /&gt;
RETAIN PD200801-PD200807 2.2222 A1 'xxxxxx';&lt;BR /&gt;
RUN;&lt;BR /&gt;
proc sql noprint;                                 &lt;BR /&gt;
  select name into :PDLIST separated by ' '       &lt;BR /&gt;
        from dictionary.columns                   &lt;BR /&gt;
        where upcase(libname) = 'WORK'            &lt;BR /&gt;
              and upcase(memname) = 'TEST_DATA'     &lt;BR /&gt;
              and upcase(name) like 'PD%'         &lt;BR /&gt;
              order by name descending;           &lt;BR /&gt;
quit;                                             &lt;BR /&gt;
%put sorted var list (descending): &amp;amp;pdlist;&lt;BR /&gt;
proc print u ;&lt;BR /&gt;
id a1; var &amp;amp;pdlist;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
    <pubDate>Fri, 01 Aug 2008 17:54:45 GMT</pubDate>
    <dc:creator>sbb</dc:creator>
    <dc:date>2008-08-01T17:54:45Z</dc:date>
    <item>
      <title>sorting variable names in a sas dataset ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-variable-names-in-a-sas-dataset/m-p/35657#M7054</link>
      <description>i have 100 variables in a sas dataset.&lt;BR /&gt;
i want to see those variables in ascending order of their names, in that dataset.&lt;BR /&gt;
&lt;BR /&gt;
note that, i am trying to sort variable names, instead of observations(proc sort).&lt;BR /&gt;
&lt;BR /&gt;
is it possible in sas?&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;another doubt:-&lt;/B&gt;&lt;BR /&gt;
&lt;BR /&gt;
in those 100 variables, i want to see 98th variable as 2nd variable.&lt;BR /&gt;
becas it is frequently used one.&lt;BR /&gt;
means, placing perticular variable at required position.&lt;BR /&gt;
&lt;BR /&gt;
plz help me !&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
regards,&lt;BR /&gt;
pavan.</description>
      <pubDate>Fri, 01 Aug 2008 12:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-variable-names-in-a-sas-dataset/m-p/35657#M7054</guid>
      <dc:creator>Pavan_SAS</dc:creator>
      <dc:date>2008-08-01T12:15:38Z</dc:date>
    </item>
    <item>
      <title>Re: sorting variable names in a sas dataset ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-variable-names-in-a-sas-dataset/m-p/35658#M7055</link>
      <description>Well not easy to do you can have a macro do it. &lt;BR /&gt;
&lt;BR /&gt;
by creating a new table in proc sql you can reorder the columns.  See the below macro for guidance.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
%macro ReorderColumns(in_lib,input_dataset_name, output_dataset);&lt;BR /&gt;
&lt;BR /&gt;
  proc sql;&lt;BR /&gt;
    create table VARNAMES as&lt;BR /&gt;
      select upcase(NAME) as NAME, varnum&lt;BR /&gt;
        from DICTIONARY.COLUMNS&lt;BR /&gt;
        where libname=upcase("&amp;amp;in_lib") and memname=upcase("&amp;amp;input_dataset_name")&lt;BR /&gt;
	    order by upcase(NAME);&lt;BR /&gt;
  quit;&lt;BR /&gt;
&lt;BR /&gt;
  data _null_;&lt;BR /&gt;
    set VARNAMES end=eof;&lt;BR /&gt;
	if not eof then call symput('varname'||strip(put(_n_,8.)),strip(name)||',');&lt;BR /&gt;
	  else do;&lt;BR /&gt;
	    call symput('varname'||strip(put(_n_,8.)),strip(name));&lt;BR /&gt;
		call symputx('numvar',_n_);&lt;BR /&gt;
	  end;&lt;BR /&gt;
  run;&lt;BR /&gt;
&lt;BR /&gt;
  proc sql;&lt;BR /&gt;
    create table &amp;amp;output_dataset as&lt;BR /&gt;
	  select %do i=1 %to &amp;amp;numvar;&lt;BR /&gt;
	            &amp;amp;&amp;amp;varname&amp;amp;i&lt;BR /&gt;
			 %end;&lt;BR /&gt;
	    from &amp;amp;in_lib..&amp;amp;input_dataset_name ;&lt;BR /&gt;
  quit;&lt;BR /&gt;
&lt;BR /&gt;
%mend ReorderColumns;&lt;BR /&gt;
&lt;BR /&gt;
%ReorderColumns(sashelp,shoes,work.test);&lt;BR /&gt;
&lt;BR /&gt;
proc print data=sashelp.shoes;&lt;BR /&gt;
proc print data=test;run;&lt;BR /&gt;
&lt;BR /&gt;
Regards!&lt;BR /&gt;
-Darryl</description>
      <pubDate>Fri, 01 Aug 2008 13:07:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-variable-names-in-a-sas-dataset/m-p/35658#M7055</guid>
      <dc:creator>darrylovia</dc:creator>
      <dc:date>2008-08-01T13:07:07Z</dc:date>
    </item>
    <item>
      <title>Re: sorting variable names in a sas dataset ?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/sorting-variable-names-in-a-sas-dataset/m-p/35659#M7056</link>
      <description>Here is some PROC SQL code I frequently use to generate a sorted variable list of periods (PDyymmdd -- output from PROC TRANSPOSE) as columns, output as a SAS macro variable.  Do consider that there is a max length for a macro variable, which may limit use of this technique:&lt;BR /&gt;
&lt;BR /&gt;
data TEST_DATA;&lt;BR /&gt;
RETAIN PD200801-PD200807 2.2222 A1 'xxxxxx';&lt;BR /&gt;
RUN;&lt;BR /&gt;
proc sql noprint;                                 &lt;BR /&gt;
  select name into :PDLIST separated by ' '       &lt;BR /&gt;
        from dictionary.columns                   &lt;BR /&gt;
        where upcase(libname) = 'WORK'            &lt;BR /&gt;
              and upcase(memname) = 'TEST_DATA'     &lt;BR /&gt;
              and upcase(name) like 'PD%'         &lt;BR /&gt;
              order by name descending;           &lt;BR /&gt;
quit;                                             &lt;BR /&gt;
%put sorted var list (descending): &amp;amp;pdlist;&lt;BR /&gt;
proc print u ;&lt;BR /&gt;
id a1; var &amp;amp;pdlist;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 01 Aug 2008 17:54:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/sorting-variable-names-in-a-sas-dataset/m-p/35659#M7056</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2008-08-01T17:54:45Z</dc:date>
    </item>
  </channel>
</rss>

