1
votes

Suppose I have a dataset with 100 columns. Is there a way I can create an array for all of those columns and still retain the variable names?

I know you can do this:

 array var{*} n1-n100 ;

but I want to keep the original variables names. Is there a way you can do this? I basically want to iterate through the array and keep only a select few variables.

3

3 Answers

2
votes

If all variables are the same type, i.e. numeric or character you can use the short hand notation of numeric or character

array var(*) _numeric_;
2
votes

If the variables that you want to reference form an unbroken range in your column order (and are all of the same type), you can use a double-dash list to put them into an array, referencing only the first and last variable. E.g.

data _null_;
    set sashelp.class(obs = 1);
    array example[*] age--weight;
    dim = dim(example);
    put dim=;
run;
1
votes

If we assume this is coming from your other question (About reading in a .CSV and keeping only a few columns), there is an interesting solution, assuming your data isn't too wild and crazy.

First, a few notes. You can't conditionally KEEP variables with just data step code.

if x>1 then keep y;

That doesn't work. It will always keep y. The keep/drop/rename (and some other statements, called compile-time statements, are not allowed to depend on the data: their effect is defined during the compilation of the data step. This goes into relatively complex SAS details, but the long and short of it is, you can't use Drop, Keep, Rename conditionally without dipping into the macro language.

Second, note that if you know the names of the columns you want to keep, just put them in a keep statement. Iterating through an array isn't going to help you here, unless you're trying to do it conditionally by some sort of data value, which as above isn't directly possible.

However, if you're reading in from a CSV file (or any other input method), you can do something not all that different, which will do exactly what you want it to, and allow you to effectively read in a delimited file by selecting specific columns. I posted that part as an answer to the master duplicate here from your other question, as it's really more appropriate there (given your question here doesn't reference CSV files or anything else like that), but I suspect it would be useful.