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;
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;
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.
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;
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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.