Mohit GUpta
Mohit GUpta

Reputation: 11

SAS Output Not Correct

My concern is about the piece of code I am executing in Odamid. Code is:

data b;
if id lt 3 then output b;
input id name $ class $;
datalines;
1 Sudha A
2 Gaurav B
3 Saurabh C
4 Preeti D
;
run;

This code is giving me empty numeric dataset with 5 observation.

I am concerned why it is giving me 5 obs. why not not 4?

Upvotes: 1

Views: 66

Answers (2)

Tom
Tom

Reputation: 51621

You are getting 5 observations because the data step iterates five times. On the fifth time it stops when the INPUT statement reads past the end of the data.

All of the variables are empty because you executed the output statement before you executed the input statement.

Normally SAS adds an implicit output statement at the end of your data step. But when the step already has an explicit output statement it does not add one.

Upvotes: 1

Richard
Richard

Reputation: 27518

There are 5 observations output because

  • the explicit conditional output is before input
  • conditional is always true

In knowing

  • At top of DATA step loop all non-retained variables are reset to missing
  • if id < 3 evaluation is . < 3, which is true, and output occurs
  • input implicitly checks for end of data condition, if true the step terminates
  • input parses data line an assigns variable values
  • bottom of step reached

In checking

data b;
put 'NOTE: ' _n_= 'before if';
if id lt 3 then output b;
put 'NOTE: ' _n_= 'before input';
input id name $ class $;
put 'NOTE: ' _n_= 'before datalines';
datalines;
1 Sudha A
2 Gaurav B
3 Saurabh C
4 Preeti D
;
run;
---------------- LOG -----------------
NOTE: _N_=1 before if
NOTE: _N_=1 before input
NOTE: _N_=1 before datalines
NOTE: _N_=2 before if
NOTE: _N_=2 before input
NOTE: _N_=2 before datalines
NOTE: _N_=3 before if
NOTE: _N_=3 before input
NOTE: _N_=3 before datalines
NOTE: _N_=4 before if
NOTE: _N_=4 before input
NOTE: _N_=4 before datalines
NOTE: _N_=5 before if
NOTE: _N_=5 before input
NOTE: The data set WORK.B has 5 observations and 3 variables.

Recommended reading:

  • "A SAS® Programmer's View of the of the SAS Supervisor" Ian Whitlock, Westat Inc., SUGI 22 Conference Proceedings, 1997.

    Abstract

    This tutorial answers questions like:

    • As a DATA step programmer, what do I need to know about the SAS supervisor and why?
    • How does the SAS supervisor process DATA step code?
    • How does a SAS MERGE work?
    • What about engines, indexing, and views?
    • What happens when my DATA step code contains macro variables?
    • What if my DATA step invokes a macro or is contained in a macro?

Upvotes: 3

Related Questions