Please correct my understanding of the datastep process in the code?

Reply
Frequent Contributor
Posts: 137

Please correct my understanding of the datastep process in the code?

Hey SAS Experts,

Please correct my understanding of the PDV in the below code:

The program runs perfectly fine.

data have;

input id $ date value holddate holdvalue lastdate lastvalue;

informat date holddate lastdate ddmmyy10.;

format date holddate lastdate ddmmyy10.;

datalines;

A 7/10/2014 11840 7/10/2014 11840 4/10/2014 15280

;

data need;

set have;

if date ne lastdate+1 then do date=lastdate+1 to holddate-1;

      value=lastvalue;

      output;

     end;

     putlog _all_; /* I tried looking at the PDV details, still haven't understood though */

     value=holdvalue;

  lastdate=date;

  lastvalue=value;

  output;

run;

Where does it say in the program after the conditional do loop to get the date value to 7/10/2014 as it outputs exactly what is needed?I.e My point is after the execution of the do loop, shouldn't date have the value of 6/10/2014 in the PDV? how did it correctly change to 7/10/2014 after i.e during the execution of 2nd output statement?

I know I am breaking my head for something simple, but your quick help would mean a lot to me.

Many thanks,

Charlotte

Super User
Posts: 9,878

Re: Please correct my understanding of the datastep process in the code?

Very Interesting Question.

do date=lastdate+1 to holddate-1;    <=>   do date=lastdate+1 to holddate-1  by 1;

Maybe date will add one automatically  when it reach holddate-1 . therefore date actually would be holddate-1+1 = holddate .

Anyway it is good know this about SAS syntax.

Xia Keshan

Respected Advisor
Posts: 3,790

Re: Please correct my understanding of the datastep process in the code?

I think this illustrates how it works more clearly than your example.  The index is incremented and then compared to stop leaving the value of I at 4 when the loop is finished.  You could use UNTIL to make it stop and leave the index eq to stop..

data _null_;
  
do i = 1 to 3;
     
put i=;
      end;
  
put i=;
   run;

i=
1
i=
2
i=
3
i=
4


data _null_;
  
do i = 1 to 3 until(i eq 3);
      put i=;
      end;
  
put i=;
   run;

i=
1
i=
2
i=
3
i=
3
Ask a Question
Discussion stats
  • 2 replies
  • 255 views
  • 4 likes
  • 3 in conversation