BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi All,

I have the data as following: lastsell with some zero values. I just want to replace all zero value with last available data. Eg. From 0 to 40 and 0 to 37.5

please note that i have already input the data into sas file.

security_symbol dtstart dtend lastsell
AA 03JAN07:10:00:00 03JAN07:11:00:00 40
AA 03JAN07:11:00:00 03JAN07:12:00:00 0
AA 03JAN07:14:30:00 03JAN07:15:30:00 0
AA 03JAN07:15:30:00 03JAN07:16:30:00 40
AA 04JAN07:10:00:00 04JAN07:11:00:00 37.5
AA 04JAN07:11:00:00 04JAN07:12:00:00 0
AA 04JAN07:14:30:00 04JAN07:15:30:00 0
AA 04JAN07:15:30:00 04JAN07:16:30:00 0

I want to use retain and lag function to do the coding, but just cannot make it.

Please give me a favor to suggest me the coding. Your help is always appreciated. Thank you very much.

Regards,
Chaoji
2 REPLIES 2
Patrick
Opal | Level 21
data have;
infile datalines truncover dlm=' ';
input security_symbol $ dtstart anydtdtm17. dtend anydtdtm17. lastsell ;
format dtstart dtend datetime21.;
datalines;
AA 03JAN07:10:00:00 03JAN07:11:00:00 40
AA 03JAN07:11:00:00 03JAN07:12:00:00 0
AA 03JAN07:14:30:00 03JAN07:15:30:00 0
AA 03JAN07:15:30:00 03JAN07:16:30:00 40
AA 04JAN07:10:00:00 04JAN07:11:00:00 37.5
AA 04JAN07:11:00:00 04JAN07:12:00:00 0
AA 04JAN07:14:30:00 04JAN07:15:30:00 0
AA 04JAN07:15:30:00 04JAN07:16:30:00 0
;
run;

data want;
set have;
retain LastSellRet 0;
lagLastsell=lag(lastsell);
if lastsell ne 0 then LastSellRet=Lastsell;
else if lastsell=0 and lagLastsell ne 0 then LastSellRet=lagLastsell;
run;

proc print data=want;
run;

HTH
Patrick
deleted_user
Not applicable
I think my question was not clear. My data has already in SAS file as you can see in following link. Just additional data i wanna add.

http://images.temppic.com/29-03-2010/images_vertis/1269866814_0.84240500.jpg

Anyway,Thank you very much for your answer Patrick. Your help is always appreciated.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

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