algorithms: reference to the previous observation

  • Follow


Let's say that I have a dataset with 2 columns: observation (from 1 to
n) and x (a continuous variable from 0 to 100).

I want to write the following algorith:

if x (obs=i) <20 and x ( obs = i-1) is > 20 then y = 1;
else y =0;

where y would be a new column with value 0 or 1, and "i" is a
particular observation.

In other words, for each row or observation, if x <20 and the previous
value for x (that is the value of x of the previous observation or
row) is > 20, then y would get a 1, otherwise y would be 0.

Thanks in advance!
0
Reply nuria 8/26/2010 12:50:04 AM

On Aug 25, 5:50=A0pm, nuria <nchapi...@yahoo.com> wrote:
> Let's say that I have a dataset with 2 columns: observation (from 1 to
> n) and x (a continuous variable from 0 to 100).
>
> I want to write the following algorith:
>
> if x (obs=3Di) <20 and x ( obs =3D i-1) is > 20 then y =3D 1;
> else y =3D0;
>
> where y would be a new column with value 0 or 1, and "i" is a
> particular observation.
>
> In other words, for each row or observation, if x <20 and the previous
> value for x (that is the value of x of the previous observation or
> row) is > 20, then y would get a 1, otherwise y would be 0.
>
> Thanks in advance!

if x < 20 and lag(x) > 20 then y=3D1; else y=3D0;
0
Reply Ya 8/26/2010 12:59:54 AM


On Aug 25, 8:59=A0pm, Ya <huang8...@gmail.com> wrote:
> On Aug 25, 5:50=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
>
>
>
>
> > Let's say that I have a dataset with 2 columns: observation (from 1 to
> > n) and x (a continuous variable from 0 to 100).
>
> > I want to write the following algorith:
>
> > if x (obs=3Di) <20 and x ( obs =3D i-1) is > 20 then y =3D 1;
> > else y =3D0;
>
> > where y would be a new column with value 0 or 1, and "i" is a
> > particular observation.
>
> > In other words, for each row or observation, if x <20 and the previous
> > value for x (that is the value of x of the previous observation or
> > row) is > 20, then y would get a 1, otherwise y would be 0.
>
> > Thanks in advance!
>
> if x < 20 and lag(x) > 20 then y=3D1; else y=3D0;

Or since SAS evaluates boolean expressions as 1 for true and 0 for
false you do not need the IF statement.

y =3D ( x<20 and lag(x) > 20 );
0
Reply tom.abernathy (199) 8/26/2010 5:16:53 AM

On Aug 25, 10:16=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrote:
> On Aug 25, 8:59=A0pm, Ya <huang8...@gmail.com> wrote:
>
>
>
> > On Aug 25, 5:50=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
> > > Let's say that I have a dataset with 2 columns: observation (from 1 t=
o
> > > n) and x (a continuous variable from 0 to 100).
>
> > > I want to write the following algorith:
>
> > > if x (obs=3Di) <20 and x ( obs =3D i-1) is > 20 then y =3D 1;
> > > else y =3D0;
>
> > > where y would be a new column with value 0 or 1, and "i" is a
> > > particular observation.
>
> > > In other words, for each row or observation, if x <20 and the previou=
s
> > > value for x (that is the value of x of the previous observation or
> > > row) is > 20, then y would get a 1, otherwise y would be 0.
>
> > > Thanks in advance!
>
> > if x < 20 and lag(x) > 20 then y=3D1; else y=3D0;
>
> Or since SAS evaluates boolean expressions as 1 for true and 0 for
> false you do not need the IF statement.
>
> y =3D ( x<20 and lag(x) > 20 );

That is great!

Lag is to refer to the previous observation, how do you refer to the
next one?
0
Reply nchapinal (89) 8/26/2010 9:48:22 PM

Getting the NEXT observation is not really part of the language.
You can trick it.

data new;
   merge old old(firstobs=3D2 keep=3Dx rename=3D(x=3Dlead_x) );
   y =3D max(x,lag(x),lead_x);
run;


On Aug 26, 5:48=A0pm, nuria <nchapi...@yahoo.com> wrote:
> On Aug 25, 10:16=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrote:
>
>
>
>
>
> > On Aug 25, 8:59=A0pm, Ya <huang8...@gmail.com> wrote:
>
> > > On Aug 25, 5:50=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
> > > > Let's say that I have a dataset with 2 columns: observation (from 1=
 to
> > > > n) and x (a continuous variable from 0 to 100).
>
> > > > I want to write the following algorith:
>
> > > > if x (obs=3Di) <20 and x ( obs =3D i-1) is > 20 then y =3D 1;
> > > > else y =3D0;
>
> > > > where y would be a new column with value 0 or 1, and "i" is a
> > > > particular observation.
>
> > > > In other words, for each row or observation, if x <20 and the previ=
ous
> > > > value for x (that is the value of x of the previous observation or
> > > > row) is > 20, then y would get a 1, otherwise y would be 0.
>
> > > > Thanks in advance!
>
> > > if x < 20 and lag(x) > 20 then y=3D1; else y=3D0;
>
> > Or since SAS evaluates boolean expressions as 1 for true and 0 for
> > false you do not need the IF statement.
>
> > y =3D ( x<20 and lag(x) > 20 );
>
> That is great!
>
> Lag is to refer to the previous observation, how do you refer to the
> next one?

0
Reply tom.abernathy (199) 8/26/2010 11:03:19 PM

Or, take a look at:
http://www.sascommunity.org/wiki/Look-Ahead_and_Look-Back

HTH,
Art
-----------
On Aug 26, 7:03=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrote:
> Getting the NEXT observation is not really part of the language.
> You can trick it.
>
> data new;
> =A0 =A0merge old old(firstobs=3D2 keep=3Dx rename=3D(x=3Dlead_x) );
> =A0 =A0y =3D max(x,lag(x),lead_x);
> run;
>
> On Aug 26, 5:48=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
>
>
> > On Aug 25, 10:16=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrote:
>
> > > On Aug 25, 8:59=A0pm, Ya <huang8...@gmail.com> wrote:
>
> > > > On Aug 25, 5:50=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
> > > > > Let's say that I have a dataset with 2 columns: observation (from=
 1 to
> > > > > n) and x (a continuous variable from 0 to 100).
>
> > > > > I want to write the following algorith:
>
> > > > > if x (obs=3Di) <20 and x ( obs =3D i-1) is > 20 then y =3D 1;
> > > > > else y =3D0;
>
> > > > > where y would be a new column with value 0 or 1, and "i" is a
> > > > > particular observation.
>
> > > > > In other words, for each row or observation, if x <20 and the pre=
vious
> > > > > value for x (that is the value of x of the previous observation o=
r
> > > > > row) is > 20, then y would get a 1, otherwise y would be 0.
>
> > > > > Thanks in advance!
>
> > > > if x < 20 and lag(x) > 20 then y=3D1; else y=3D0;
>
> > > Or since SAS evaluates boolean expressions as 1 for true and 0 for
> > > false you do not need the IF statement.
>
> > > y =3D ( x<20 and lag(x) > 20 );
>
> > That is great!
>
> > Lag is to refer to the previous observation, how do you refer to the
> > next one?- Hide quoted text -
>
> - Show quoted text -

0
Reply art297 (4237) 8/26/2010 11:33:29 PM

On Aug 26, 4:33=A0pm, Arthur Tabachneck <art...@netscape.net> wrote:
> Or, take a look at:http://www.sascommunity.org/wiki/Look-Ahead_and_Look-B=
ack
>
> HTH,
> Art
> -----------
> On Aug 26, 7:03=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrote:
>
> > Getting the NEXT observation is not really part of the language.
> > You can trick it.
>
> > data new;
> > =A0 =A0merge old old(firstobs=3D2 keep=3Dx rename=3D(x=3Dlead_x) );
> > =A0 =A0y =3D max(x,lag(x),lead_x);
> > run;
>
> > On Aug 26, 5:48=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
> > > On Aug 25, 10:16=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrote:
>
> > > > On Aug 25, 8:59=A0pm, Ya <huang8...@gmail.com> wrote:
>
> > > > > On Aug 25, 5:50=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
> > > > > > Let's say that I have a dataset with 2 columns: observation (fr=
om 1 to
> > > > > > n) and x (a continuous variable from 0 to 100).
>
> > > > > > I want to write the following algorith:
>
> > > > > > if x (obs=3Di) <20 and x ( obs =3D i-1) is > 20 then y =3D 1;
> > > > > > else y =3D0;
>
> > > > > > where y would be a new column with value 0 or 1, and "i" is a
> > > > > > particular observation.
>
> > > > > > In other words, for each row or observation, if x <20 and the p=
revious
> > > > > > value for x (that is the value of x of the previous observation=
 or
> > > > > > row) is > 20, then y would get a 1, otherwise y would be 0.
>
> > > > > > Thanks in advance!
>
> > > > > if x < 20 and lag(x) > 20 then y=3D1; else y=3D0;
>
> > > > Or since SAS evaluates boolean expressions as 1 for true and 0 for
> > > > false you do not need the IF statement.
>
> > > > y =3D ( x<20 and lag(x) > 20 );
>
> > > That is great!
>
> > > Lag is to refer to the previous observation, how do you refer to the
> > > next one?- Hide quoted text -
>
> > - Show quoted text -

Thanks for the link!

I was wondering if there was the an equivalent function to "lag" to
refer the next observation. I guess not, otherwise I would have found
it by now... however, it is surprising!
Does anyone know if such a function exists:
0
Reply nchapinal (89) 8/27/2010 5:36:24 PM

On Aug 27, 1:36=A0pm, nuria <nchapi...@yahoo.com> wrote:
> On Aug 26, 4:33=A0pm, Arthur Tabachneck <art...@netscape.net> wrote:
>
>
>
>
>
> > Or, take a look at:http://www.sascommunity.org/wiki/Look-Ahead_and_Look=
-Back
>
> > HTH,
> > Art
> > -----------
> > On Aug 26, 7:03=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrote:
>
> > > Getting the NEXT observation is not really part of the language.
> > > You can trick it.
>
> > > data new;
> > > =A0 =A0merge old old(firstobs=3D2 keep=3Dx rename=3D(x=3Dlead_x) );
> > > =A0 =A0y =3D max(x,lag(x),lead_x);
> > > run;
>
> > > On Aug 26, 5:48=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
> > > > On Aug 25, 10:16=A0pm, Tom Abernathy <tom.aberna...@gmail.com> wrot=
e:
>
> > > > > On Aug 25, 8:59=A0pm, Ya <huang8...@gmail.com> wrote:
>
> > > > > > On Aug 25, 5:50=A0pm, nuria <nchapi...@yahoo.com> wrote:
>
> > > > > > > Let's say that I have a dataset with 2 columns: observation (=
from 1 to
> > > > > > > n) and x (a continuous variable from 0 to 100).
>
> > > > > > > I want to write the following algorith:
>
> > > > > > > if x (obs=3Di) <20 and x ( obs =3D i-1) is > 20 then y =3D 1;
> > > > > > > else y =3D0;
>
> > > > > > > where y would be a new column with value 0 or 1, and "i" is a
> > > > > > > particular observation.
>
> > > > > > > In other words, for each row or observation, if x <20 and the=
 previous
> > > > > > > value for x (that is the value of x of the previous observati=
on or
> > > > > > > row) is > 20, then y would get a 1, otherwise y would be 0.
>
> > > > > > > Thanks in advance!
>
> > > > > > if x < 20 and lag(x) > 20 then y=3D1; else y=3D0;
>
> > > > > Or since SAS evaluates boolean expressions as 1 for true and 0 fo=
r
> > > > > false you do not need the IF statement.
>
> > > > > y =3D ( x<20 and lag(x) > 20 );
>
> > > > That is great!
>
> > > > Lag is to refer to the previous observation, how do you refer to th=
e
> > > > next one?- Hide quoted text -
>
> > > - Show quoted text -
>
> Thanks for the link!
>
> I was wondering if there was the an equivalent function to "lag" to
> refer the next observation. I guess not, otherwise I would have found
> it by now... however, it is surprising!
> Does anyone know if such a function exists:

There is not a function, that is why the other solutions have been
created.
For most real world problems it is not needed.
Perhaps if you describe your problem someone can suggest another way
to store the data that will make the calculations easier.
0
Reply tom.abernathy (199) 8/27/2010 11:56:08 PM

7 Replies
202 Views

(page loaded in 0.107 seconds)

Similiar Articles:













7/23/2012 4:19:49 AM


Reply: