I have a number of files I merged to have a single final dataset. While doing this, sas ordered the variables as follows: n_myvar1_NUM, n_myvar10_NUM, n_myvar11_NUM, ..... I tried to order the dataset with retain function to get the following corrected order: n_myvar1_NUM, n_myvar2_NUM, n_myvar3_NUM etc, but without success.
What is the best way to order variables 1, 2, 3, instead of 1, 10, 11, etc? Overall variables span from 1 to 100, so I have: n_myvar1_NUM, ..., n_myvar100_NUM.
Thank you in advance
It is much easier to deal with numbered lists of variables when the number as at the END of the name. You can use variable lists when referencing the variables. And many procedures that order the variables alphabetically, like PROC CONTENTS, will treat the numeric suffix as a human would. For example if I make dataset with variables named var1 to var10 but create them in some other order:
data have;
length var2 var4 var1 var10 var3 var5 var8 var9 var7 var6 8;
run;
And then run PROC CONTENTS on the dataset it will list them from 1 to 10 by default.
But if I append a suffix to the names then that does not happen.
If instead you are talking about the variables actual position in the dataset (what PROC CONTENTS shows when you add the VARNUM option) then the easiest way to combine a lot of datasets and get the variables in position order you want to is have some template or skeleton dataset that you have already created and include that as the first dataset.
data want;
set skeleton(obs=0) ds1 ds2 dslast ;
run;
And also again having numbers at the ends of the variables will also make it easier to use other methods of setting the variable order. For example you can use the RETAIN statement (in a very simple data step) to force the variable order.
data want;
retain var1-var10;
set have ;
run;
This works because the RETAIN statement forces the data step compiler to add the variables to the dataset, but it does not yet force the complier to decide what type (and length) they should have. So when they finally appear in the dataset referenced by the SET statement the compiler will determine the type then.