Date: Thu, 25 Aug 2011 14:04:10 -0700
Reply-To: "Nordlund, Dan (DSHS/RDA)" <NordlDJ@DSHS.WA.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Nordlund, Dan (DSHS/RDA)" <NordlDJ@DSHS.WA.GOV>
Subject: Re: Lag values
In-Reply-To: <201108252029.p7PJNvIk031408@waikiki.cc.uga.edu>
Content-Type: text/plain; charset=utf-8
data want;
input id year quarter variablex ;
lag_variablex = lag(variablex);
cards;
1 2000 1 10 .
1 2000 2 5 10
1 2000 3 . 5
1 2000 4 20 .
;
run;
proc print;
run;
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> sas8832@HOTMAIL.CO.UK
> Sent: Thursday, August 25, 2011 1:30 PM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Re: Lag values
>
> panel dataset
>
> id year quarter variablex lag_variablex
> 1 2000 1 10 .
> 1 2000 2 5 10
> 1 2000 3 . 5
> 1 2000 4 20 .
>
> When variablex is missing for the previous quarter, i want
> lag_variablex=.
> rather than equal to the previous available value for variable x. I
> have
> already filled holes (ie missing quarters).
>
> Thanks
Here is one possibility:
data have;
input id year quarter variablex ;
cards;
1 2000 1 10
1 2000 2 5
1 2000 3 .
1 2000 4 20
2 2000 1 10
2 2000 2 5
2 2000 3 .
2 2000 4 20
;
run;
data want;
set have ;
by id;
lag_variablex = lag(variablex);
if first.id then lag_variablex = .;
run;
proc print;
run;
I added some data for another id and set the lagged value to missing when first.id = 1. If you don't want that, just delete the if statement.
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
|