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

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:


                            11020 30999 50
                            21020 30999 50
                            31020 30999 50
                            41020 30  . 50
                            51020 30  . 50
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.

View solution in original post

23 REPLIES 23
Reeza
Super User

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.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

sorry,i dont understand Smiley Happy

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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?

art297
Opal | Level 21

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.

Reeza
Super User

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.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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?

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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?

art297
Opal | Level 21

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.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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?

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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.

art297
Opal | Level 21

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; 
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

art297
Opal | Level 21

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.

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!

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
  • 23 replies
  • 1792 views
  • 9 likes
  • 4 in conversation