DATA Step, Macro, Functions and more

lag

Reply
Super Contributor
Posts: 673

lag

In the example below the physician has three records.I would like to populate the zip which has max mg for the same physician.So in this case, zip 00972 has the max mg.

physician mg zip
Peter 100 00972
peter 50 00976
peter 25 (blank)

The output :
physician mg zip
Peter 100 00972
peter 50 00972
peter 25 00972

So that I ca roll up at physician and zip level for the total mg.

I tried using this(after sorting):

data temp;
set master_cvv;
by correct_name city state zip;
if first.correct_name then x= zip;
else y = lag(X);
if x= '' then x= y;
zip1 = x;
run;

the final output looks like:

physician mg zip
Peter 100 00972
peter 50
peter 25

Two records,zip appears blank.
Any help in on this?
SAS Super FREQ
Posts: 8,864

Re: lag

Hi:
The correct method of using LAG is to call it on every row and then adjust the returned value according to your condition. As it explains in the comment to the program in this note:
http://support.sas.com/kb/24/665.html


/* This example shows the difference in output when you use conditional */
/* and unconditional logic in your program. 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. (SAS Language Reference, */
/* Dictionary --> Functions and CALL Routines --> LAG function, Example 2) */


Here are some other notes about using LAG:
http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000212547.htm
http://support.sas.com/kb/24/694.html

If you continue to have difficulty with using the LAG function, you may wish to work with Tech Support on this problem.

cynthia
Super Contributor
Posts: 673

Re: lag

Posted in reply to Cynthia_sas
Thank you !
Ask a Question
Discussion stats
  • 2 replies
  • 118 views
  • 0 likes
  • 2 in conversation