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.

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
  • 2 replies
  • 640 views
  • 0 likes
  • 2 in conversation