## Do Loop

Solved
Super Contributor
Posts: 464

# Do Loop

hi guys,

I have the code and some of the output below.If someone could explain the 5th observation from the output  please?

I was expecting to see

10  20  30 999  50  but probably retain issue?

Thanks people

------------------------------------------

 data old; input a b c d e; datalines; 10  20  30 999  50 50  40 999  20  10 999   1   1   1 999 ; run;

data new;

 array x(5) a b c d e; set old; do i=1 to 5; if x(i) eq 999 then x(i) = .;

output;

 end; drop i;

proc print;

 run;

 -----------------------------------------------------------------------------------------

and here is some of the output:

 1 10 20 30 999 50 2 10 20 30 999 50 3 10 20 30 999 50 4 10 20 30 . 50 5 10 20 30 . 50

Accepted Solutions
Solution
‎10-21-2012 06:16 PM
Super User
Posts: 23,776

## Re: Do Loop

the do loop goes through the 5 numbers. When it hits the 4th number, the 999, it sets it to missing. Then the 5th number is checked, and its not 999 so it doesn't get set to missing. However the 4th was missing so it still is missing.

Basically your code loops through the 5 numbers and if any is 999 it sets it to missing for the rest of the loop. Not sure why you want to output it for every line, but that's what you're doing.

All Replies
Super User
Posts: 23,776

## Re: Do Loop

That makes sense, the values don't reset until the hit the end of the datastep, but since you're outputting within the loop they aren't reset, because it hasn't hit the end of the datastep.

Don't drop the i if that helps you to visualize it.

Super Contributor
Posts: 464

## Re: Do Loop

sorry,i dont understand

Super Contributor
Posts: 464

## Re: Do Loop

once the x(i) hits the missing value it never changes during each observation regardless of whether the if statement is true or not.Why is that so?

PROC Star
Posts: 8,169

## Re: Do Loop

Why would you expect to see "10  20  30 999  50" when your code is requiring "10  20  30 . 50"?

Do you really want 5 records output for each record?  If not, remove the output; statement.

Solution
‎10-21-2012 06:16 PM
Super User
Posts: 23,776

## Re: Do Loop

the do loop goes through the 5 numbers. When it hits the 4th number, the 999, it sets it to missing. Then the 5th number is checked, and its not 999 so it doesn't get set to missing. However the 4th was missing so it still is missing.

Basically your code loops through the 5 numbers and if any is 999 it sets it to missing for the rest of the loop. Not sure why you want to output it for every line, but that's what you're doing.

Super Contributor
Posts: 464

## Re: Do Loop

i do get the right result without the "output" but i just wanted to play with it and understand it more.So i realize that once a value is set to missing it remains missing till the loop hits another observation  but it just did not make sense to me .Is this because of the "output" statement?

Super Contributor
Posts: 464

## Re: Do Loop

i mean if x(5) =50 and not 999 does not that mean x(5)  is different then missing based on the if statement or does the missing values overrules the if statement?

PROC Star
Posts: 8,169

## Re: Do Loop

if x(5)=50 then the code won't change its value.  The code will only change the value of x(whatever) if x(whatever)=999.

Super Contributor
Posts: 464

## Re: Do Loop

right right,x(5) was not changed i just made  a  typo posting my message

Super Contributor
Posts: 464

## Re: Do Loop

oh wait ,when the loop checks the if statement does it check it against the original observation or each time it checks it against the last observation written out?

Super Contributor
Posts: 464

## Re: Do Loop

probably  a stupid question but otherwise if the observation is 10  20  30 999  50 and if statement not true then i dont see why x(4) would be till missing.

PROC Star
Posts: 8,169

## Re: Do Loop

 data old; input a b c d e; datalines; 10  20  30 999  50 50  40 999  20  10 999   1   1   1 999 ; run;

data new;

 array x(5) a b c d e; set old; do i=1 to 5;

if x(i) eq 999 then x(i) = .;

putlog _all_;

 end; drop i; run;
Super Contributor
Posts: 464

## Re: Do Loop

i changed the missing value to a "0" value and same scenario.So looks like for x(5) the loop checks the if statement against observation #4 not #1.Thank you guys

PROC Star
Posts: 8,169

## Re: Do Loop

I think you may not understand what is happening.  The loop isn't checking across observations, it is check the five values, in each observation, or a, b, c, d and e.

🔒 This topic is solved and locked.