Help using Base SAS procedures

variables in a-z manner

Reply
Regular Contributor
Posts: 229

variables in a-z manner

hi i am haivng variables .i wan them in a-z manner.

data fin;
input w a x z p;
cards;
1 2 3 5 6
run;

i wna them in a-z manner
a p w x z

how can i do it.
Super Contributor
Posts: 321

Re: variables in a-z manner

Hi R_Win,

try this one.

proc transpose data=fin out=trans;
var _all_;
run;

proc sort data=trans;
by _name_;
run;

proc transpose data=trans out=trans2;
run;


Regards,
Milton
Respected Advisor
Posts: 3,777

Re: variables in a-z manner

The data set flip-flop using TRANSPOSE is a useful technique. However, it will cause all variables to be converted to character if any variables in the VAR list are character. The conversion to character can be a useful but that is another subject.

You can use DICTIONARY.COLUMNS to produce a alphabetical list of variables. Although there will be problems with that if any of the variables are members of an enumerated variable lists. For example B1-B10 will come out B1 B10 B2 and so on. PROC CONTENTS produces the correct ordering.


[pre]
data fin;
input w a x z p;
B5=1;
array b[10];
cards;
1 2 3 5 6
;;;;
run;
proc sql noprint;
select name into :alphaList separated by ' '
from dictionary.columns
where libname eq 'WORK' and memname eq 'FIN'
order by 1
;
quit;
run;
/* use ALPHALIST as you see fit */
%put NOTE: ALPHALIST=&alphalist;


/* correct order for alphalist with enumerated variables list*/

proc contents noprint order=ignorecase out=alphalist(keep=name);
run;

proc sql noprint;
select name into :alphaList separated by ' '
from alphalist
;
quit;
run;
/* use ALPHALIST as you see fit */
%put NOTE: ALPHALIST=&alphalist;
[/pre]
Trusted Advisor
Posts: 2,113

Re: variables in a-z manner

Another approach is to populate the "program data vector" with the names in the order that you want before doing the input statement. (search the documentation for 'program data vector' for more than you may want to know; there are some kinks if you mix numeric and character variables).

In your example, adding a KEEP statement before the INPUT will produce the desired results.

data fin;
KEEP a p w x z;
input w a x z p;
cards;
1 2 3 5 6
run;
Respected Advisor
Posts: 3,777

Re: variables in a-z manner

KEEP wont do that. I think I may have in the "old" days V5 and before. No way to test that now.

[pre]
data fin;
KEEP a p w x z;
put (_all_)(=); ** Shows that no variables are defined at this point in program*;
input w a x z p;
cards;
1 2 3 5 6
;
run;
proc contents varnum;
run;
[/pre]
Trusted Advisor
Posts: 2,113

Re: variables in a-z manner

You are showing my age!

Replacing the KEEP with a LENGTH statement will work.
LENGTH a p w x z 4;
Super Contributor
Posts: 359

Re: variables in a-z manner

There was a long thread or 2 about this a while back. RETAIN works without having to specify the variable type.
N/A
Posts: 0

Re: variables in a-z manner

Hi to all,

try to see this

Sample 24696: Reordering variables to be in alphabetical order

http://support.sas.com/kb/24/696.html
Occasional Contributor
Posts: 5

Re: variables in a-z manner

/*Put all the variables into Alphabetical order;*/

data fin;
input w a x z p;
cards;
1 2 3 5 6
run;

proc sql noprint;
select name into: new separated by ','
from dictionary.columns where libname='WORK' and memname='FIN'
order by name;
create table fin_1 as select &new. from fin;
run;

Read more : http://studysas.blogspot.com/2009/07/dictionary-tables-and-sashelp-views.html

Sarath
Ask a Question
Discussion stats
  • 8 replies
  • 137 views
  • 0 likes
  • 7 in conversation