BookmarkSubscribeRSS Feed
varunnakra
Fluorite | Level 6

data want(drop=type);

  retain Course Professor;

  input type $1. @;

  if type='C' then

  input course $ professor $;

  else if type='S' then

  /*do;*/

  input Name $10. Id;

  /*output schedule;

  end;*/

datalines;

c maths profA

s maria 100

s smith 101

c english profB

s rick 101

;

When the above code is run there we get the first obs w/o the name of the student and his id or they are blank. The next observations retain the course name and the prof name and have the name of the student and his/her id.

If we omit the comments around the do end loop and around the output statement; we don't get the first obs with the blank values of the student and his id.

I have some questions around these

1. how is the iteration of the data step related to the processing of input statements? If we don't specify an output statement explicitly the data step, shouldn't the data step produce an output and move to the next iteration once it encounters no other input statement?

2. even if we specify an explicit output statement (as done by removing the comments in the above code), why is it that we get an output only when the student information is read from the input buffer. AND, that happens only when do end loop is present. Why is that so?

Please advise!

7 REPLIES 7
Peter_C
Rhodochrosite | Level 12

if you are running base SAS interactively, you can take advantage of the data step debugger with this small change to your data statement

   data want(drop=type) /debug ;

Then you can examine the values as each statement is executed and see how values are set to missing at the start of the step unless (like course and professor) they are included on the retain statement.

The debugger command

   L  i

reports the state of the infile/input position

It would be a good case to work on.. Hope you are able to do that.

peterC

art297
Opal | Level 21

and, in addition to Peter's suggestion, take a look at

SAS(R) 9.2 Language Reference: Concepts, Second Edition

The iterations are not controlled by the input statement but, rather, the data statement itself.

varunnakra
Fluorite | Level 6

Thanks for the suggestions Arthur and Peter.

I looked at the data step debugging commands and ran some of them but not much success. I also looked at the usual control flow of a data step.

The real puzzle is with the do end loop that is there in the conditional logic of if type ='s'. If I omit the do end loop and simply specify an output statement (or choose to even omit that as well) the data step gives an output where the student information is blank which makes sense also as it never went into the branch of if type ='s' to encounter an output statement. It just executed all the statements and produced a default output.

however, with the do end loop, i dont get the default output from the data step which i should have as even the loop is mentioned in the conditional logic of if type ='s'.

hence, this is still confounding for me...

Please advise more !

Peter_C
Rhodochrosite | Level 12

three  things

1

output SCHEDULE when your data statement defined WANT

2

input name $10.

causes a problem because it takes 10 and not just the length of the name.

(a : will help --->read doc on input modifiers)

3

unless you direct it, SAS won't convert lowcase to upper, so test for the C or c as appropriate

... then it worked for me, see below

         

data schedule(drop=type);

  retain Course Professor;

  input type $1. @;

  if type='c' then  input course $ professor $ ;

  else do ;

      input Name : $10. Id;

      output schedule;

  end;

datalines;

c maths profA

s maria 100

s smith 101

c english profB

s rick 101

;

varunnakra
Fluorite | Level 6

Thanks Peter, however, the problem is not that it doesn't work when i omit the do end loop but the problem is that it works differently.

w/o the do end loop the output is

course professor Name Id

maths  profA     

maths  profA       maria 100

maths  profA       smith 101

english profB      

english profB       rick    101

w/ the do end loop the output is

course professor Name Id

maths  profA       maria 100

maths  profA       smith 101

english profB       rick    101

The moot point is why are the two obs with the values of name and id missing displayed as an o/p without the do end loop but not with the do end loop.

What makes the data step not to produce an output after the first iteration when we're using the do end loop or am i missing something here?

Peter_C
Rhodochrosite | Level 12

The most basic of descriptions of OUTPUT statement must clarify that an explicit OUTPUT removes the implict output that takes effect at end-of-step.

It is not about conditional code. It is whether the OUTPUT statement iis present among executable code  (it is ignored within a comment block as  you would expect

varunnakra
Fluorite | Level 6

Thanks Peter. Actually, I knew that whenever an explicit output statement is specified, the default output action of a data step is over ruled, however, the part that i was missing here was the do end loop. The if then construct needs a do end loop to make the output statement a part of itself. Otherwise, if the output statement is kept outside then it implies an output every time. Hence, the importance of do end loop. Yes, the point to be noted is that, during compilation itself SAS gets to know that there is an explicit output statement whether it is in the conditional code or otherwise and hence it suppresses the default output action of the data step.

Resolved!

Thanks

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 7 replies
  • 1171 views
  • 8 likes
  • 3 in conversation