Date: Wed, 21 Apr 1999 15:28:35 GMT
Reply-To: David Booth <dkb@CIX.COMPULINK.CO.UK>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: David Booth <dkb@CIX.COMPULINK.CO.UK>
Organization: Compulink Information eXchange
Subject: Re: How to dedect frequent sign changes
Wenyu asks:
> I am trying to write an efficient SAS code to handle the following question.
>
> Suppose I have a SAS dataset created with ID number and a variable x.
> Observations of x consist of consecutive positive numbers followed by
> consecutive negative numbers, and then maybe consecutive positive numbers
> again. in fact, this is a large data set so such sign changes may occur more
> than a number of times. The dataset may look something like this:
>
>
> 1 2
> 2 3
> 3 -1
> 4 -2
> 5 7
> 6 8
> 7 -9
> 8 -9
>
> What I want to do is to output the observation whenever its sign changes,
> i.e., its sign is different from its previous one----such as observation 3,
5,
> and 7 in the above dataset.
>
> Can somebody suggest an efficient code for solving this problem?
How about this? You say your data is already in a SAS dataset, so the second
data
step is all you need:
data series;
input id x;
cards;
1 2
2 3
3 -1
4 -2
5 7
6 8
7 -9
8 -9
;
run;
data changes;
set series;
drop delta;
delta = dif(x);
if delta > x > 0
or delta < x < 0;
run;
Notice that this won't write the first observation - your example looks as
though
this is as you want the code to behave. If you have missing values for x in
the
data, the above code may not do what you want, so post again if necessary and
let
us know how you want missing values to be handled. It may be enough just to
ignore
them by coding:
set series(where=( x ne .));
HTH
Dave
.
|