DATA Step, Macro, Functions and more

variable order in dataset

Reply
Frequent Contributor
Posts: 100

variable order in dataset

Hi Experts:

What to do when you have to define a new variable's length in a data step but don't want it to be the first variable in the new dataset?

In the example below, I don't want State to be the first variable. But I don't really know how to move it to be after address2 or move it to be the last variable. (I guess I could define every variable in the Length statement but what if there are many variables in the dataset?)

Thank you!

Data work.talent2;
Length State $ 3;
Set sasuser.talent(obs=5);
State=scan(address2,2,',');
Run;
Proc print data=work.talent2;
Run;
Super Contributor
Super Contributor
Posts: 365

Re: variable order in dataset

Hello Mnew,

To order variables in a dataset you can use a RETAIN statement listing variables in the order that you need. This statement should be placed before SET stetment, like this:
[pre]
data a;
retain A,B,C,D;
set a;
run;
[/pre]
Sincerely,
SPR
Regular Contributor
Posts: 241

Re: variable order in dataset

...
> To order variables in a dataset you can use a RETAIN
> statement listing variables in the order that you
> need. This statement should be placed before SET
> stetment, like this:
> [pre]
> data a;
> retain A,B,C,D;
> set a;
> run;
> [/pre]
...
@SPR: No. Retain does not take a comma delimited list of variables. Hardly any statements do. Use any of the variable lists.

For the re-ordering purpose, use retain before set and don't use length statements at all. Hope this helps.
Super Contributor
Super Contributor
Posts: 365

Re: variable order in dataset

Hello Chang_y_chung@hotmail.com,

It was typo from my side. Of course, comma is forbidden. The rest is true. RETAIN can be used to order variables in the dataset.

Sincerely,
SPR
Frequent Contributor
Posts: 100

Re: variable order in dataset

Thanks for the follow up. I did test Retain (w/o comma) and it worked for me. As for Length statement in the original question, I did have to define length for the variable (not for the re-ordering).
Super Contributor
Super Contributor
Posts: 365

Re: variable order in dataset

I forgot to add that if you need LENGTH then place if after RETAIN before SET.

SPR
Frequent Contributor
Posts: 100

Re: variable order in dataset

This is a good idea esp. when I want the new variable to be in the middle, not at the end of existing one. I'm glad that I don't have to type up all the variables in the Retain statement, if I just want this new one to be No.4.
Thank you!
Super User
Posts: 10,505

Re: variable order in dataset

Put the LENGTH statement AFTER the SET but before where the variable is used. The new variables will be at the "end" of the data.
Frequent Contributor
Posts: 100

Re: variable order in dataset

Tested. Good idea. Thank you!
Valued Guide
Posts: 632

Re: variable order in dataset

Variable order on the PDV is determined by the order that the variables are encountered during the compilation of the DATA step. As shown in the previous responses there are several ways to specify the variable in such a way as to determine the order. The SET (and MERGE etc.) statements grab the order from the incoming data set (for the variables not already seen). The FORMAT and ATTRIB statements can also be used.
Frequent Contributor
Posts: 100

Re: variable order in dataset

Thank you for explaining the why and how! Now I know why our trainer spent so much time explaining the compilation phase.
Ask a Question
Discussion stats
  • 10 replies
  • 194 views
  • 0 likes
  • 5 in conversation