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

I have data like:

ID height1 height2 height3

1 100 101 102

2 103 104 105

3 . 102 103

4 104 . 106

 

For code A, those observations after missing values cannot be output. But code B works. Can someone helps me explain it why? THX.

 

 

Code A:

Data test1;
Set example;
Array DA height:;
Do over DA;
coding=DA;
if NOT missing(coding);
output;
End;
Run;

 

Code B:

Data test2;
Set example;
Array DA height:;
Do over DA;
coding=DA;
if NOT missing(coding) then output;
End;
Run;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

The subsetting if jumps immediately to the "top" of the data step and starts the next iteration; the IF THEN OUTPUT makes only the output conditional, but will still execute all following statements in the current iteration.

 

See this code example:

data have;
input x1;
datalines;
1
2
3
4
5
;

data want1;
set have;
if x1 ne 4 then output;
x2 + 1;
run;

data want2;
set have;
if x1 ne 4;
x2 + 1;
run;

View solution in original post

3 REPLIES 3
ballardw
Super User

The first code example is a "subsetting if". It basically means that ONLY the records that pass the if condition are kept. So as soon as one of the missing values is encountered the RECORD is discarded from further processing.

 

 

The second is a conditional output statement and writes to the output set when true.

 

Kurt_Bremser
Super User

The subsetting if jumps immediately to the "top" of the data step and starts the next iteration; the IF THEN OUTPUT makes only the output conditional, but will still execute all following statements in the current iteration.

 

See this code example:

data have;
input x1;
datalines;
1
2
3
4
5
;

data want1;
set have;
if x1 ne 4 then output;
x2 + 1;
run;

data want2;
set have;
if x1 ne 4;
x2 + 1;
run;
Tom
Super User Tom
Super User

Do you want to output ALL of the non-missing values?  TEST1 will stop as soon as any value is missing.  That is because the subsetting IF statement stop the current iteration, which in this case stops the DO loop.

 

More complex test data will make this difference clearer in the output;

data example;
  input id height1-height3;
cards;
0 100 101 102
1 .   104 105
2 .   .   108
3 .   .   . 
4 112 .   .
5 115 116 .
;
TEST1
Obs    id    height1    height2    height3    coding

 1      0      100        101        102        100
 2      0      100        101        102        101
 3      0      100        101        102        102
 4      4      112          .          .        112
 5      5      115        116          .        115
 6      5      115        116          .        116

TEST2
Obs    id    height1    height2    height3    coding

 1      0      100        101        102        100
 2      0      100        101        102        101
 3      0      100        101        102        102
 4      1        .        104        105        104
 5      1        .        104        105        105
 6      2        .          .        108        108
 7      4      112          .          .        112
 8      5      115        116          .        115
 9      5      115        116          .        116
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
  • 3 replies
  • 1087 views
  • 3 likes
  • 4 in conversation