BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RohanSmith
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

7 REPLIES 7
Astounding
PROC Star

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.

PGStats
Opal | Level 21

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
art297
Opal | Level 21

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?

Cynthia_sas
SAS Super FREQ


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

art297
Opal | Level 21

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.

Cynthia_sas
SAS Super FREQ

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

RohanSmith
Calcite | Level 5

Hi,

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

Cheers

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1199 views
  • 0 likes
  • 5 in conversation