Hi all,
I'm trying to subset a large dataset that has a couple sequentially named variables (e.g. dx1-dx15, proc1-proc15). I did not create these variables, but am wondering if I can reference them in a do loop in order to conditionally check their contents without declaring them as an array - or if there is another manner to do so.
At this moment I've been writing separate if statements to check each variable individually or including each variable and its condition separately in a where statement, which has worked thus far. I'm just wondering if there's a more elegant way to code it.
Thanks!
@jdchang wrote:
Hi all,
I'm trying to subset a large dataset that has a couple sequentially named variables (e.g. dx1-dx15, proc1-proc15). I did not create these variables, but am wondering if I can reference them in a do loop in order to conditionally check their contents without declaring them as an array - or if there is another manner to do so.
At this moment I've been writing separate if statements to check each variable individually or including each variable and its condition separately in a where statement, which has worked thus far. I'm just wondering if there's a more elegant way to code it.
Thanks!
I would say that it would be better to use an explicitly defined array. Since the array definition accepts things like
array d dx1-dx15;
to define the array then there isn't much extra work involved.
Second then you have tools involving arrays like the VNAME function. So suppose I am checking some conditions on an array such as
the value should be in a range of values then I can do something like:
do i=1 to dim(d); if d[i] < 10 then do: var= vname(d[i]); put var +1 "is < 10 on record " _n_; end; if d[i] > 50 then do: var= vname(d[i]); put var +1 "is > 50 on record " _n_; end; end;
If a group of variables have similar required tests they can all be in an array used for that test. So if both your DX and PROC variables should not have any missing you could have them in a single array just for that test assuming they are of the same variable type.
Why don't you want to put them in an array? Seems like the way to go
I'd be fine with doing that. Could I use the already established variable names when creating the array so that the variable names don't change? These are also formatted variables and it'd be great if I could keep them as is, but if there's no avoiding it then I'll bite the bullet.
Neither variable names nor formats will change 🙂 Arrays are just groupings of variables for smarter processing.
Do something along these line 🙂
data want;
set have;
array dx {*} dx1-dx15;
do i=1 to dim(dx);
if dx[i] = 0 then output; /* Or do something else */
end;
run;
Thanks! I've only ever used the array statement to create variables and wasn't sure if it could be used to reference already established variables.
@jdchang wrote:
Hi all,
I'm trying to subset a large dataset that has a couple sequentially named variables (e.g. dx1-dx15, proc1-proc15). I did not create these variables, but am wondering if I can reference them in a do loop in order to conditionally check their contents without declaring them as an array - or if there is another manner to do so.
At this moment I've been writing separate if statements to check each variable individually or including each variable and its condition separately in a where statement, which has worked thus far. I'm just wondering if there's a more elegant way to code it.
Thanks!
I would say that it would be better to use an explicitly defined array. Since the array definition accepts things like
array d dx1-dx15;
to define the array then there isn't much extra work involved.
Second then you have tools involving arrays like the VNAME function. So suppose I am checking some conditions on an array such as
the value should be in a range of values then I can do something like:
do i=1 to dim(d); if d[i] < 10 then do: var= vname(d[i]); put var +1 "is < 10 on record " _n_; end; if d[i] > 50 then do: var= vname(d[i]); put var +1 "is > 50 on record " _n_; end; end;
If a group of variables have similar required tests they can all be in an array used for that test. So if both your DX and PROC variables should not have any missing you could have them in a single array just for that test assuming they are of the same variable type.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.