BookmarkSubscribeRSS Feed
Smitha9
Fluorite | Level 6

Hi,

I have a dataset with variables:

ID Name DOB Place Status

I want a variable "Status" to place after DOB

ID Name DOB Status Place

Can I make a code saying variable Status after DOB? or at 4th position?

 

Please let me know. thanks.

 

3 REPLIES 3
mklangley
Lapis Lazuli | Level 10
proc sql;
    create table want as
    select id
          ,name
          ,dob
          ,status
          ,place
    from have;
quit;

or

data want;
    retain id name dob status;
    set have;
run;

 

mkeintz
PROC Star

First, you can't move a variable in a data set without making a new copy.  Here's one way to make that copy, taking advantage of the sas data step compiler:

 

data want;
  merge have (keep=ID Name DOB ) have (keep=status) have;     /*corrected typo */
run;

The SAS compiler will arrange variables in the order they are revealed in the code.  So:

  1. First merge have has ID NAME DOB  (but ordered in whatever relative positions they have in the original).
  2.  Second merge have gets STATUS, placed to the right of the above.
  3. Third merge have gets all the remaining variables, in their original order, placed to the right of STATUS.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
andreas_lds
Jade | Level 19

The position of variable in a dataset is completely irrelevant for processing the data, with one exception: proc export. For anything else: don't waste your time!

But if you insist, here is another way to re-order the variables:

 

data work.class;
  format Name Weight Age Sex Height;
  set sashelp.class;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 558 views
  • 0 likes
  • 4 in conversation