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;
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
...
> 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.
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).
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!
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.