BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jdchang
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

 

View solution in original post

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Why don't you want to put them in an array? Seems like the way to go

jdchang
Fluorite | Level 6

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.

PeterClemmensen
Tourmaline | Level 20

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;
jdchang
Fluorite | Level 6

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. 

ballardw
Super User

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

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 916 views
  • 3 likes
  • 3 in conversation