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 |
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.
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.
sorry,i dont understand
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?
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.
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.
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?
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?
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.
right right,x(5) was not changed i just made a typo posting my message
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?
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.
Run the following, then look at your log. I think it will answer your question:
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; |
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
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.
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.
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.