BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
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?
2 REPLIES 2
Cynthia_sas
Diamond | Level 26
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
SASPhile
Quartz | Level 8
Thank you !

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1282 views
  • 0 likes
  • 2 in conversation