Help using Base SAS procedures

HELP!! Problem in using lag function in a Do-until statement

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

HELP!! Problem in using lag function in a Do-until statement

I was trying to write a loop as follows:

data ...;

set ...;   (a variable called attendance is included)

temp=1;

do until (temp=5);

prev_attend=lag(attendance);

if prev_attend=temp then attendance=prev_attend+1;

temp+1;

end;

run;

I was hoping to have the prev_attend updated each time the loop starts again, however this does not seem possible? Any suggestions?

Thanks!


Accepted Solutions
Solution
‎09-15-2012 11:46 AM
Super User
Posts: 5,498

Re: HELP!! Problem in using lag function in a Do-until statement

Posted in reply to RohanSmith

Looking at the whole program, there is probably an easier way to accomplish your overall objective.  But since you only asked about incrementing prev_attend, here's a good way to do it.  It may or may not work, depending on what you are trying to assign to PREV_ATTEND.  This will get the final value of ATTENDANCE from the previous observation, after the DO loop has completed.

Remove this from the DO loop:

prev_attend=lag(attendance);

ADD this statement BEFORE the SET statement:

prev_attend=attendance;

That should be one way to handle it.

If you are looking to get the ORIGINAL value of ATTENDANCE, from BEFORE the DO loop started, then just place the LAG function OUTSIDE of the DO loop:

prev_attend=lag(attendance);

do until (temp=5);

Good luck.

View solution in original post


All Replies
Solution
‎09-15-2012 11:46 AM
Super User
Posts: 5,498

Re: HELP!! Problem in using lag function in a Do-until statement

Posted in reply to RohanSmith

Looking at the whole program, there is probably an easier way to accomplish your overall objective.  But since you only asked about incrementing prev_attend, here's a good way to do it.  It may or may not work, depending on what you are trying to assign to PREV_ATTEND.  This will get the final value of ATTENDANCE from the previous observation, after the DO loop has completed.

Remove this from the DO loop:

prev_attend=lag(attendance);

ADD this statement BEFORE the SET statement:

prev_attend=attendance;

That should be one way to handle it.

If you are looking to get the ORIGINAL value of ATTENDANCE, from BEFORE the DO loop started, then just place the LAG function OUTSIDE of the DO loop:

prev_attend=lag(attendance);

do until (temp=5);

Good luck.

Respected Advisor
Posts: 4,920

Re: HELP!! Problem in using lag function in a Do-until statement

Posted in reply to RohanSmith

It is not entirely clear what you want but it might be simply this :

data ...;

set ...;   (a variable called attendance is included)

prev_attend=lag(attendance);

do temp = 1 to 5;

     if prev_attend=temp then attendance=prev_attend+1;

     end;

run;

PG

PG
PROC Star
Posts: 7,468

Re: HELP!! Problem in using lag function in a Do-until statement

Posted in reply to RohanSmith

Can you provide a small sample dataset, in the form of a data step, as well as what you would want that dataset to look like after the code has been run?

SAS Super FREQ
Posts: 8,864

Re: HELP!! Problem in using lag function in a Do-until statement

Posted in reply to RohanSmith


Hi:

  Read the comments in this Tech Support sample code carefully:

http://support.sas.com/kb/24/665.html

  The note contains an example of using the LAG function and in the program code, it says:

"Because the LAG function stores values on the queue only when it is called, you must call LAG unconditionally (outside the IF condition) to get the correct answers."

(emphasis mine)

cynthia

PROC Star
Posts: 7,468

Re: HELP!! Problem in using lag function in a Do-until statement

Posted in reply to Cynthia_sas

Cynthia:  The reason I asked Rohan to explain what he was looking for is that there are exceptions to that rule.  Take a look at: http://www.sascommunity.org/wiki/Conditional_Use_of_LAG .  Additionally, he was calling it five times within the same record.  I, for one, have absolutely NO idea what he is expecting.

SAS Super FREQ
Posts: 8,864

Re: HELP!! Problem in using lag function in a Do-until statement

Hi, Art:

  I know there are exceptions. However, generally, I try to keep my suggestions at the same approximate skill level as the posted code. Given the fact that calling the LAG function inside the DO loop didn't seem appropriate and that the code was written in a way that there was NO idea what the input was or what the desired result was, I gave a link that (I hoped) would prompt further investigation into the basics of the LAG function. Then, after the basics are understood, that's the time for exceptions. (IMHO)

  Thanks for the link.

cynthia

New Contributor
Posts: 2

Re: HELP!! Problem in using lag function in a Do-until statement

Posted in reply to RohanSmith

Hi,

The suggestions from Astounding worked for me. All comments here have been greatly appreciated.

Cheers

🔒 This topic is solved and locked.

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

Discussion stats
  • 7 replies
  • 312 views
  • 0 likes
  • 5 in conversation