DATA Step, Macro, Functions and more

Simple RETAIN Problem

Reply
Occasional Contributor
Posts: 9

Simple RETAIN Problem

I am solving this problem but have got stuck.Can anyone help me out.

Thanks .

Super User
Posts: 19,822

Re: Simple RETAIN Problem

Please include the question directly in the post. Users will not download attachments.  

Super User
Super User
Posts: 7,060

Re: Simple RETAIN Problem

So it really depends on what they mean by "after the third observation has been read".

image.png

First I would use the term observations to refer to records in a SAS dataset, but this program is reading lines from a raw data file.

Second do they mean after the third line is read and before the assignment statement executes? or after the assignment statement has executed?

 

The value of COUNT when the third line is read by the INPUT statement will be 130 since the value that was output at the end of the second iteration of the data step is still there since it is being retain. You get to 130 by adding the 10 and 20 to the initial value of 100.

 

Now what the result will be after the assignment statement executes depends on how the source file is formatted. If the third line has at least three spaces on it (something that CANNOT be determined from the photograph you posted) then TENS will be missing and COUNT will stay at 130.

 

But if the third line has less than three characters on it then SAS will go to the next line to find two characters and so TENS will be 4 and the COUNT total with 134.

 

Try it yourself.

filename dat temp;
data _null_;
  input str $3. ;
  file dat ;
  len=lengthn(str);
  put @2 str $varying2. len ;
cards;
 10
 20

 40
 50
;
run;
data test;
  infile dat ;
  input tens 2-3 ;
  retain count 100;
  count + tens;
  put (_n_ tens count) (=) ;
run;

data _null_;
  input str $char3. ;
  file dat ;
  len=lengthn(str);
  put str $char3. ;
cards;
 10
 20

 40
 50
;
run;
data test;
  infile dat ;
  input tens 2-3 ;
  retain count 100;
  count + tens;
  put (_n_ tens count) (=) ;
run;

 

 

Occasional Contributor
Posts: 9

Re: Simple RETAIN Problem

Hey Thankyou so much  Tom for a detailed explanation ..

Super User
Posts: 19,822

Re: Simple RETAIN Problem

@rajvir Please mark Tom's solution as the correct answer (not this post!).

Ask a Question
Discussion stats
  • 4 replies
  • 118 views
  • 1 like
  • 3 in conversation