How can I put variables in the positions I require using SAS code?
when I use a merge, variables are positioned by default
In the dataset I can move columns / variables and change them posicision, but how to do it directly by code?
you can do it by:
data want;
retain ... variable names in desired order ...;
set have;
... your code ...
run;
you can do it by:
data want;
retain ... variable names in desired order ...;
set have;
... your code ...
run;
An "empty" format statement also works (only variable names, but no formats). Consider this if you want to prevent the main effect that retain has on variables that are not supplied by the contributing datasets.
@Kurt_Bremser you are right, I have tested it without and with a given format after the variables list
and it worked fine in both cases.
It worked only if the format statement was before the set:
data want;
format x1-x5 3. ;
set have;
run;
I used the term "empty" format statement as that does not override the attributes of variables coming from datasets, if those need to be preserved.
And yes, any "variable-ordering" statement has to come first in the data step. Doing it after the set (eg) means that the original order in the contributing datasets will take precedence. It's just how the data step compiler creates the PDV while scanning the data step code.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.