BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

another method:

data want;
   format Origin Length Wheelbase Weight mpg_highway;
   set sashelp.cars;
run;

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

Rarely is re-arranging the order of variables necessary. Why do you need to do this?

--
Paige Miller
purpleclothlady
Pyrite | Level 9

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

purpleclothlady
Pyrite | Level 9

Hi All:

I wander if there is any other ways to rearrange var. order in Data step ?

  • Methods:
  • ATTRIB statement but we have to put length, format... . , which is not handy.
data attrib_method;
    attrib day month year date sales length=3;
    set sashelp.retail(obs=5);
run;
  • If we use RETAIN before SET statement, will this sometimes accidentally change the variable values
    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

Astounding
PROC Star

"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.

purpleclothlady
Pyrite | Level 9
Hi Astounding:
thanks for clarification. I will test it.
Tom
Super User Tom
Super User

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;
purpleclothlady
Pyrite | Level 9
Hi Tom:
here is an example:
data cars;
set sashelp.cars;
run;

/*---WANT--- THE LAST 5 VARIABLES: ORIGIN, LENGTH, WHEELBASE,WEIGT ,MPG_HIGHWAY TO BE SHOWN AS THE FIRST 5 VARIABLES*/
data template;
input ORIGIN $ LENGTH WHEELBASE WEIGT MPG_HIGHWAY ;
datalines;
run;
data want;
set template(obs=0) cars;
run;
Question:
There are mixed char and numeric , I would need to specify that in input statement.
Is this the right way of creating template?

thank you
Tom
Super User Tom
Super User

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;
andreas_lds
Jade | Level 19

another method:

data want;
   format Origin Length Wheelbase Weight mpg_highway;
   set sashelp.cars;
run;
tarheel13
Rhodochrosite | Level 12

I like format or retain best.

purpleclothlady
Pyrite | Level 9

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)

 

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 11 replies
  • 1957 views
  • 7 likes
  • 6 in conversation