another method:
data want;
format Origin Length Wheelbase Weight mpg_highway;
set sashelp.cars;
run;
Rarely is re-arranging the order of variables necessary. Why do you need to do this?
Hi Paige:
I am working on large datasets with lots of variables, some data sets need to open in data step such as : subject, date, order...
thank you,
Purple
Hi All:
I wander if there is any other ways to rearrange var. order in Data step ?
data attrib_method;
attrib day month year date sales length=3;
set sashelp.retail(obs=5);
run;
data retain_method;
retain day month year date sales;
set sashelp.retail;
run;
However, unless the sole purpose of a data step is to re-order variables, the use of RETAIN for this purpose can lead to unwanted results when the value of any ‘retained’ variable is carried over from one data step iteration to another.
Are there any other ways?
thanks all in advance.
purple
"Carryover" of values is not an issue when you use a SET statement. ALL variables from a SAS data set are automatically retained. So adding a RETAIN does not change that feature, and does not change which variables are being retained.
One simple way to insure that the dataset is defined as you want is to have a template or skeleton dataset that you can use to set the variables.
data want;
set template(obs=0) have;
run;
INPUT statement is for reading in values, not for DEFINING the variables.
I would use LENGTH or ATTRIB to define the variables.
data template ;
length origin $6 length wheelbase weight mpg_highway 8;
label
length = 'Length (IN)'
wheelbase = 'Wheelbase (IN)'
weight = 'Weight (LBS)'
mpg_highway = 'MPG (Highway)'
;
stop;
call missing(of _all_);
run;
And yes if your template defines the variable as numeric and the dataset you are trying to modify has it as character that is an ERROR that you would want the code to detect.
Another way to impact the order is using the KEEP= (or DROP=) dataset option.
data want;
set
sashelp.cars(keep=origin)
sashelp.cars(keep=length)
sashelp.cars(keep=wheelbase)
sashelp.cars(keep=weight)
sashelp.cars(keep=mpg_highway)
sashelp.cars
;
run;
another method:
data want;
format Origin Length Wheelbase Weight mpg_highway;
set sashelp.cars;
run;
I like format or retain best.
Hi all:
Thanks all for your help.
They all work in Data Step but depending on your preference .
Using 1. Retain(with caution), 2. Format, 3. Adding a Template dataset , 4. Attrib, 5. Keep/drop (this one will create more obs than original)
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.