DATA Step, Macro, Functions and more

why does do while retain values from if then.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

why does do while retain values from if then.

Can anybody give me the explanation to why the values nn2 and n3 are retained in this do while statement?

 

data test;
n=0;
   do while(n<5);
      put n=;
      n+1;
	  if n=1 then n2=1;
	  if n=2 then n3=2;
	  output;
	end;
run;

Udklip.PNG


Accepted Solutions
Solution
‎09-18-2017 08:08 AM
Super Contributor
Posts: 331

Re: why does do while retain values from if then.

Hello,

 

The PDV, that is, the vector holding values for the currently processed observation is only cleared

when the interpreter meets the run instruction. Here, you are always processing the same observation that

you modify and output several times. There is no reason for the values of n2 and n3 to be reset.

View solution in original post


All Replies
SAS Super FREQ
Posts: 497

Re: why does do while retain values from if then.

You have a one pass data step.  It never goes back for a second pass becasue there is not input data set.  Nothing is retained (in the normal SAS sense).  Nothing is reinitialized to missing for the second pass, because there is no second pass.

Occasional Contributor
Posts: 14

Re: why does do while retain values from if then.

Posted in reply to WarrenKuhfeld

Are you saying it is because sas keeps n2 and n3 in memory and then keeps priting the same values?

Solution
‎09-18-2017 08:08 AM
Super Contributor
Posts: 331

Re: why does do while retain values from if then.

Hello,

 

The PDV, that is, the vector holding values for the currently processed observation is only cleared

when the interpreter meets the run instruction. Here, you are always processing the same observation that

you modify and output several times. There is no reason for the values of n2 and n3 to be reset.

Super User
Posts: 13,358

Re: why does do while retain values from if then.

Maybe this adds a bit to what goes on:

data test;
n=0;
   do while(n<5);
      put n=;
      n+1;
	  if n=1 then n2=1;
          else n2=.;
	  if n=2 then n3=2;
          else n3=.;
	  output;
	end;
run;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 136 views
  • 1 like
  • 4 in conversation