Date: Tue, 7 Oct 2008 19:07:46 -0400
Reply-To: "Howard Schreier <hs AT dc-sug DOT org>"
<schreier.junk.mail@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Howard Schreier <hs AT dc-sug DOT org>"
<schreier.junk.mail@GMAIL.COM>
Subject: Re: Missing variables
On Mon, 6 Oct 2008 20:23:56 -0400, Lou <lpogoda@HOTMAIL.COM> wrote:
>Randy wrote:
>> Dear All:
>> My data is as follows
>>
>> varID
>> 1
>> 2
>> 3
>> 4
>> 0
>> 7
>> 8
>> 0
>> 10
>> 11
>> 12
>> 0
>> 16
>> and so on
>> I need to construct another var, VARID_one
>> where
>> varid varid_one
>> 1 1
>> 2 2
>> 3 3
>> 4 4
>> 0 6
>> 7 7
>> 8 8
>> 0 9
>> 10 10
>> 11 11
>> 12 12
>> 0 15
>> 16 16
>> Where the when the value of VarID = 0 then VarID_one = the next value of
>> VarID - 1.
>> Randy
>Assuming your data are in a dataset called VARIDS, a simple merge will
>get you where you want to go:
>
>data result (drop = nextid);
>
> merge varids
>
> varids (firstobs = 2
>
> rename = (varid = nextid));
>
> if varid = 0 then varid_one = nextid - 1;
>
> else varid_one = varid;
>
>run;
>
>Caution - the final value of NEXTID will be missing, so if the final
>value of VARID is 0, then the final value of VARID_ONE will be missing
>minus one - in other words, missing.
A slightly terser variation:
data result;
merge varids
varids( firstobs = 2 rename = (varid = varid_one) );
varid_one = ifn( varid = 0 , varid_one - 1 , varid);
run;
|