DATA Step, Macro, Functions and more


Super Contributor
Posts: 712


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;

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?
Posts: 9,365

Re: lag

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:

/* 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:

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

Super Contributor
Posts: 712

Re: lag

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