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

Hi!  I am trying to figure out the proper way to complete an array do loop.  I have 10 variables within a larger table that start with DX  (DX1, DX2, DX3...to DX10).   I need to know if any values that start with '493' are in the DX5-DX10 variable range.  If yes, then output the observation.

 

Here is what I have so far (but it may all be wrong!).   I can't figure out how to output the observation... 

 

data test_dxcheck;

set have;

array dx(*) DX5-DX10;

do i= 5-10;

if dx(i)=: '493' then do;

????

 

 

Thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You haven't described exactly what you want to do when found. Output a value, write a message to the log or what.

Also what do you do if more than one of the variables has the behavior? Do the action only once or repeat for each match.

 

Your basically there though you would get an array out of bounds for i = 6 as written. Suppose you want to write a message to the log:

data test_dxcheck;
   set have;
   array dx(*) DX5-DX10;
   do i= 1 to dim(dx);
      if dx(i)=: '493' then do put "Value found at position " I  dx[I];
   end;
run;

if you want to output the observation only once for the first match

 

data test_dxcheck;
   set have;
   array dx(*) DX5-DX10;
   do i= 1 to dim(dx);
      if dx(i)=: '493' then do ;
          output;
          leave;
      end;
   end;
run;

if you want to output the data for each match remove the LEAVE statement.

 

View solution in original post

5 REPLIES 5
ballardw
Super User

You haven't described exactly what you want to do when found. Output a value, write a message to the log or what.

Also what do you do if more than one of the variables has the behavior? Do the action only once or repeat for each match.

 

Your basically there though you would get an array out of bounds for i = 6 as written. Suppose you want to write a message to the log:

data test_dxcheck;
   set have;
   array dx(*) DX5-DX10;
   do i= 1 to dim(dx);
      if dx(i)=: '493' then do put "Value found at position " I  dx[I];
   end;
run;

if you want to output the observation only once for the first match

 

data test_dxcheck;
   set have;
   array dx(*) DX5-DX10;
   do i= 1 to dim(dx);
      if dx(i)=: '493' then do ;
          output;
          leave;
      end;
   end;
run;

if you want to output the data for each match remove the LEAVE statement.

 

jenim514
Pyrite | Level 9

@ballardw Thank you!   Just needed to output the observation 🙂

 

Astounding
PROC Star

With only 6 variables to worry about, it's borderline whether you would want to bother with arrays.  You could code it simply:

 

data want;

set have;

n_493 = (dx5 =: '493') + (dx6 =: '493') + (dx7 =: '493') + (dx8 =: '493') + (dx9 =: '493') + (dx10 =: '493');

if n_493 > 0;

run;

 

Who knows, you might even find that the variable n_493 is useful later.

Jim_G
Pyrite | Level 9


data have;
input (dx1 dx2 dx3 dx4 dx5 dx6 dx7 dx8 dx9 dx10)(:$5.);
cards;
22 33 44 55 4830 4931 77 88 99 500
;
proc print;
data test_dxcheck;
set have;
array dx $5. DX1 - DX10;
do i= 5 to 10;
if dx{i}=: '493' then output;
end;
proc print; run;

 

slchen
Lapis Lazuli | Level 10

If I understand you correctly, you could try this:

data test_dxcheck;
set have;
length flag $100;
retain flag;
array dx DX5-DX9;
do i=1 to dim(dx);
  if dx(i)=:'493' then flag=catx(' ',flag,vname(dx(i)));
end;
drop i;
run; 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1201 views
  • 3 likes
  • 5 in conversation