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?)
Length State $ 3;
Proc print data=work.talent2;
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:
> 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:
> data a;
> retain A,B,C,D;
> set a;
@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.
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.
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.