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

ok ,when the loop checks for x(5) or "e" he checks into 10  20  30 999  50 or into 10  20  30 0  50?

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

i mean 10  20  30 999  50 or 10  20  30 .  50?

art297
Opal | Level 21

When the loop checks for x(5) it is only checking for e or 50.  Since 50 isn't equal to 999 it is left at 50.

art297
Opal | Level 21

It might help if you did the same thing without either a loop or array.  Your code is doing the same thing as below:

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;

set old;

if a eq 999 then a=.;

if b eq 999 then b=.;

if c eq 999 then c=.;

if d eq 999 then d=.;

if e eq 999 then e=.;

run;

First, it is doing it for the first row (i.e., 10 20 30 99 and 50), then outputting the results, then doing it for the 2nd row and outputting the results, and then again for the third row.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

i could get the results using different methods but i wanted to understand how loop works with set and at the end i got stuck.

ok,a not 999 so sas writes the observation

same for b and c.Then d=999 so it gets set to missing and sas writes that observation too then when the loops checks for e,last element not 999 nothing changes and sas writes the 5th observation but with the missing value for d again.So it looked like the loop checked 10 20 30 . 50 for the value of d and not 10 20 30 999 50. Probably i am missing something huge about do loops and the output statement.

                            11020 30999 50
                            21020 30999 50
                            31020 30999 50
                            41020 30  . 50
                            51020 30  . 50
                            65040999 20 10
                            75040999 20 10
                            85040  . 20 10
                            95040  . 20 10
                           105040  . 20 10
                           11 . 1999
                           12 . 1999
                           13 . 1999
                           14 . 1999
                           15 . .
art297
Opal | Level 21

For the first record:

when the loop checks x(i) and i=1 it is ONLY checking the value of a.  Since the loop contained an output statement it was outputing the values of a, b, c, d and e.

when the loop checks x(i) and i=2 it is ONLY checking the value of b.  Since the loop contained an output statement it was outputing the values of a, b, c, d and e.

when the loop checks x(i) and i=3 it is ONLY checking the value of c.  Since the loop contained an output statement it was outputing the values of a, b, c, d and e.

when the loop checks x(i) and i=4 it is ONLY checking the value of d.  Since the value was 999 it chances the value of d to missing.  Since the loop contained an output statement it was outputing the values of a, b, c, d and e.

Finally, when the loop checks x(i) and i=5 it is ONLY checking the value of e.  Since the loop contained an output statement it was outputing the values of a, b, c, d and e.

Then, SAS goes on to the second record, etc.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

and the value of d was set to  missing from before already and that's way that change reflects in the last output too.

Thank you both for lengthy bother Smiley Happy

Tom
Super User Tom
Super User

The SET statement is the one that is reading in values from the OLD dataset.

So on the first pass through the OLD dataset when you change D from 999 to missing it will stay missing until you read the second observation.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

yaa thanks Smiley Happy

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 23 replies
  • 3857 views
  • 9 likes
  • 4 in conversation