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.
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.
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.