BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
I have a quick question about changing the internal order of
variables in a datset.
I have a dataset with 23 variables. But I need them to be arrang in a specific order.

for instance
provider PATIENT_ID ndc_number NDC_DESC lot_No

are to be arranged as:
ndc_number NDC_DESC PATIENT_ID lot_No provider
7 REPLIES 7
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Why do you feel they need to be re-arranged, internally? You have no control now SAS stores the variables, other than when generated by-default order with say PROC PRINT and no VAR statement listed. You can influence the declared order of your SAS variables by defining a RETAIN statement (ahead of the SET, if coded) in the desired order - be careful using the RETAIN statement as has been discussed on the various SAS topic forums on the planet.

Scott Barry
SBBWorks, Inc.
SASPhile
Quartz | Level 8
Thanks Scott!
I created a dataset using the describe statement and arranged the variables of the order i want and appened the original dataset to it.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Humbly, you still have not explained why it is important to know or even care how SAS stores the information internally, even if you had the capability or control. However, as mentioned, the RETAIN statement, though I don't recommend it, will give you to establish a "default order" (not to be confused with any SAS-managed internal sequence) for the variables/columns in your SAS dataset/table.

Scott Barry
SBBWorks, Inc.
data_null__
Jade | Level 19
> Humbly, you still have not explained why it is
> important to know or even care how SAS stores the
> information internally, even if you had the
> capability or control. However, as mentioned, the
> RETAIN statement, though I don't recommend it, will
> give you to establish a "default order" (not to be
> confused with any SAS-managed internal sequence) for
> the variables/columns in your SAS dataset/table.

I think the OP means VARNUM order. We know that SAS doesn't necessary store the columns in VARNUM order but that is unimportant for this discussion.

VARNUM order is not important most of the time but there are times when clients want variables in a certain order. When that is the case I usually create the variable in the proper order. However there are times when it is necessary to move the columns around, and for that the RETAIN statement is the most concise method. I know that some SAS users have "retain-a-phobia". Perhaps the following example will help.

[pre]
proc contents data=sashelp.shoes order=varnum;
run;
data newShoes;
stop;
retain
Inventory
Region
Product
Subsidiary
Stores
Sales
Returns
;
set sashelp.shoes;
run;
data newShoes;
set newShoes sashelp.shoes;
*** Perhaps more code;
run;
proc contents data=newShoes order=varnum;
run;
[/pre]
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Wait - that really didn't feel so bad!

Thanks data _null_;

Scott
mftuchman
Quartz | Level 8
Scott is 100% right with regard to sound practice. With so many SAS reporting procedures that let you order columns as needed, there is really no reason to need this. However, an audience raised in Excel is an audience that is accustomed to moving columns around. Thus, I can see where this issue originates.

Base SAS VIEWTABLE, and SAS Enterprise Guide give a default view of the data. In some cases, I can see why people might want the most interesting variables "at the left". I've been known to force an ATTRIB statement before the SET statement so that "my favorites" come up on the left in a new view.

But for the most part I agree with Scott.

data test / view=test;
attrib age label='Age' weight label='Weight' sex label='Sex' height label='Height' name label='Name';
set sashelp.class;
run;

If you now click on WORK.TEST in the explorer, the data will appear in the order listed on the ATTRIB statement. But really, other ways are better.
mftuchman
Quartz | Level 8
These variable names seem *awfully* familiar. You work in my office? 🙂

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 796 views
  • 0 likes
  • 4 in conversation