I have several variables ordered in SAS such as this: var_10_1, var_10_2, var_1_1, var_1_2, var_2_1, var_2_2.
If I use an array statement such as the following, what position would these variables be in?
array var (1:10) var_:;
I'm thinking they are character sorted so var_10_1 is before var_1_1, but the order by which they are in the dataset (which is the same in this circumstance) could also be an option. If the latter is the case I can use a retain to re-order them, but if the former is the case I'll have to come up with a different solution to put var_1 before var_10.
Thanks for you help!
The order in the array is the order in which the variables were first created. You can check that by running a PROC CONTENTS. One of the pieces of output is the order of the variables. You can even shorten the output to refer to just the variables of interest:
proc contents data=have (keep=var_: );
run;
And as was mentioned, if you are processing the variables within a DATA step, using a DO loop and an array, the VNAME function will tell you the variable name for the current array element.
Use VNAME to test it.
Given your indexes perhasps you want a two dimensional array making it easier to know which you’re referencing?
The order in the array is the order in which the variables were first created. You can check that by running a PROC CONTENTS. One of the pieces of output is the order of the variables. You can even shorten the output to refer to just the variables of interest:
proc contents data=have (keep=var_: );
run;
And as was mentioned, if you are processing the variables within a DATA step, using a DO loop and an array, the VNAME function will tell you the variable name for the current array element.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.