Date: Wed, 13 Sep 2006 08:51:47 -0700
Reply-To: "Nordlund, Dan (DSHS)" <NordlDJ@DSHS.WA.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Nordlund, Dan (DSHS)" <NordlDJ@DSHS.WA.GOV>
Subject: Re: Lead function?
Content-Type: text/plain; charset=utf-8
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> Dunnigan, Keith
> Sent: Wednesday, September 13, 2006 8:38 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Lead function?
>
> Dear SAS-L'rs,
>
> I can state my question best with an example, the below dataset gives
> me what I want. The first 2 variables are the key ones in the input, I
> and t_i. They correspond to timepoint and the time at timepoint i in
> hours. I would prefer to calculate the second two variables which are
> t_i lagged or lead by one timepoint. SAS has a lag function which will
> give me the second variable. From what I have been able to find they
> don't have a similar 'lead' function to calculate the second one. Can
> someone give my some code to do this (the simpler the better)? I should
> say also that the number of timepoints is in general unknown (that is,
> don't hardcode a '4' into the code). The incoming dataset can vary,
> but will include I and t_i.
>
>
> ** Times dataset.;
>
>
>
> data times;
>
> input i t_i t_im1 t_ip1;
>
> cards;
>
> 1 0.5 . 1
>
> 2 1 0.5 2
>
> 3 2 1 3
>
> 4 3 2 .
>
> run;
>
>
> Thanks for any help or advice you can give!
>
> Keith Dunnigan
> Sr. Biostatistician
> Mallinckrodt Medical / Tyco Healthcare
Keith,
Here is a brute force method that is simple enough. I am sure someone will
provide a much more elegant one.
data times;
input i t_i /*t_im1 t_ip1*/;
cards;
1 0.5 . 1
2 1 0.5 2
3 2 1 3
4 3 2 .
;
run;
proc sort data=times;
by descending i;
run;
data times2;
set times ;
t_ip1=lag(t_i);
run;
proc sort data=times2;
by i;
run;
data times3;
set times2 ;
t_im1=lag(t_i);
run;
proc print data=times3;
run;
Hope this is helpful,
Dan
Daniel J. Nordlund
Research and Data Analysis
Washington State Department of Social and Health Services
Olympia, WA 98504-5204
|