BookmarkSubscribeRSS Feed
sss
Fluorite | Level 6 sss
Fluorite | Level 6
HI All,
I want to find TBF(Time Between Failure) with respect to id. Sampe data goes here...................

data new;
input id$ date:date9.;
FORMAT DATE DATE9.;
datalines;
a 15aug2007
a 21apr2009
b 05may2009
c 15may2009
d 01feb2010
d 01mar2010
;
run;

output
====================
ID date TBF
a 15aug2007 .
a 21apr2009 615
b 05may2009 .
c 15may2009 .
d 01feb2010 .
d 01mar2010 28

I tried with oracle code it is working fine .code goes here

select *, TRUNC(TO_CHAR(date- lag(date,1) OVER (PARTITION BY assetnum ORDER BY faildate)),2) AS TBF_Days from table1
by id;

but when i am tring to execute same code in proc sql then i m getting error msg like
PARTITION BY not define lag cant be used in proc sql . I think there are restriction are there .


I want to get the put using proc sql.

I tried with Lag and dif function in Data Step but not getting the output.

please help me out.
5 REPLIES 5
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello SSS,

This is a solution:
[pre]
data i;
input id$ date:date9.;
FORMAT DATE DATE9.;
datalines;
a 15aug2007
a 21apr2009
b 05may2009
c 15may2009
d 01feb2010
d 01mar2010
;
run;
proc sort data=i;
by id date;
run;
data r;
retain fd;
set i;
if FIRST.ID then fd=date;
if LAST.ID then TBF=date-fd;
if FIRST.ID and LAST.ID then TBF=.;
by ID;
drop fd;
run;
[/pre]
Sincerely,
SPR
sss
Fluorite | Level 6 sss
Fluorite | Level 6
Hey buddy
thnxs a lot it works
SPR
Quartz | Level 8 SPR
Quartz | Level 8
I am a bit surprised why do you need all those TBF=. ? May be better exclude them?

SPR
sss
Fluorite | Level 6 sss
Fluorite | Level 6
HI SPR,

I am calculating TBF for asset.
how frequently it has failed and and the time.

u can see for Asset A: it has failed for 2 time . so, i wnt 2 calculate the TBF for the asset
Asset B: its only 1 time and so on

Hey can we do same task using proc sql ? if possble plz suggest me


thnx
shivkumar
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello SSS,

This is a solution:
[pre]
proc SQL;
create table t as
select id, min(date) as min, max(date) as max
from i
group by ID
;quit;
proc SQL;
create table r as
select a.id, a.date,
case
when date=min or max-min=0 then .
else max-min
end as TBF
from i as a left join t as b
on a.ID=b.ID
order by ID, date
;quit;
[/pre]
Sincerely,
SPR

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