Discussion:
Order to columns in SAS dataset..........
(too old to reply)
Saovik
2005-12-20 13:01:06 UTC
Permalink
How do you give order to columns in a SAS dataset..........? I am
having variables like i_: etc.... how do I deal with them while giving
order............?
Richard A. DeVenezia
2005-12-20 13:52:24 UTC
Permalink
Post by Saovik
How do you give order to columns in a SAS dataset..........? I am
having variables like i_: etc.... how do I deal with them while giving
order............?
This question is asked frequently and should be found in the nonexistent SAS
FAQ. :)
Search the archives for specific examples.

When and why do you need to impose a column name order ?

When outputting in a Proc, generally you will use the VAR statement to
specify the order in which the proc presents the columns.

For viewing data of or a subset from an imperiously large and generally
non-rebuildable table use a DATA Step View with RETAIN statement, or SQL
View with SELECT clause

To recreate the entire table with your new column order, use a DATA Step
with RETAIN statement.

When the column names contain a modicum of consistent or utilizable meta
data the SQL DICTIONARY.COLUMNS table or SAS view SASHELP.VCOLUMN can be
used to create a macro variable containing a list of column names based on
some programmatic specification (e.g. sort column names alphabetically, or
by numeric suffix, etc...). The macro variable would then be coded to be
resolved in a RETAIN statement or SELECT clause at Step parse and execute
time.
--
Richard A. DeVenezia
http://www.devenezia.com/
riccardo
2005-12-20 14:29:22 UTC
Permalink
Hi there
I understand it is important to have columns in a specific order....
also for specifing intervals by separating the first and last variable
with a double dash.
I work it around by writing a proc summary statement using the var
option to give the order you want.
Another way to do it is creating an empty table with the column order
wanted and then appending the data to this new table.
Any step that re-writes the PDV to the desired order might do....

Riccardo
Post by Saovik
How do you give order to columns in a SAS dataset..........? I am
having variables like i_: etc.... how do I deal with them while giving
order............?
toby dunn
2005-12-20 15:08:02 UTC
Permalink
Riccardo,

Actually it isn't important that you have them ordered in your data set at
all. SAS has var statements that takes care of any order you need such as
with proc print. The idea I believe is to free the programmer from having
to remember that and frees them up to concentrate on their program design
and output rather than the specifics. The same concept can be applied to
SAS's by statement.

data one ;
A_3 = 1 ;
A_1 = 2 ;
A_2 = 3 ;
run ;


data twob ;
set one ;
array A (3) ;
B = sum(of A_1--A_3) ;

put _all_ ;
run ;

Does throw an error. But consider this as an alternative:
data twoa ;
set one ;
B = sum(of A_1-A_3) ;

put _all_ ;
run ;

It throw no errors.

An een better solution given that your naming schema is the same and you
want all the vars starting with some predefined set of characters to be
summed is:

data twoc ;
set one ;
B = sum(of A: ) ;

put _all_ ;
run ;


Mmuch cleaner and you end up letting SAS to al the work. All the
programmer, program reader, and/or mainatance programmer need know is that
all vars starting with 'A_' will be summed.




Now there are some people who insist on a specified order (i.e. pointy
haired bosses). All that is required is a retain statement wit hthe order
you wish the vars to be held in the PDV. This question has come up so much
in the past I won't go into the specifics of how it works but will leave
those readers who are interested to go search the archives. The only thing
you have to remember is that you need to have the retain statement before
set statement or before you reference any other variables.

Data One ;
retain I_1 I_2 I_3 I_4 I_5 ;
set aaa ;
run ;

Will set I_1--I_5 as the first five vars in the PDV regardless of the order
of the vars read from the meta data coming from data set aaa.





Toby Dunn





From: riccardo <***@GMAIL.COM>
Reply-To: riccardo <***@GMAIL.COM>
To: SAS-***@LISTSERV.UGA.EDU
Subject: Re: Order to columns in SAS dataset..........
Date: Tue, 20 Dec 2005 06:29:22 -0800
Hi there
I understand it is important to have columns in a specific order....
also for specifing intervals by separating the first and last variable
with a double dash.
I work it around by writing a proc summary statement using the var
option to give the order you want.
Another way to do it is creating an empty table with the column order
wanted and then appending the data to this new table.
Any step that re-writes the PDV to the desired order might do....

Riccardo
Post by Saovik
How do you give order to columns in a SAS dataset..........? I am
having variables like i_: etc.... how do I deal with them while giving
order............?
Loading...