Solved
Contributor
Posts: 58

# Change observations according to the previous obervation value

Hi,

I have a dataset which looks like:

data have;

infile datalines;

input numbers;

datalines;

1

1

.

2

3

4

.

5

5

6

7

7

;

In case the previous obervation is not . and the present observation equals to the previous value, then i would like to set the current value to . So what I would like to have is

data want;

infile datalines;

input numbers;

datalines;

1

.

.

2

3

4

.

5

.

6

7

.

;

Can someone help me solve this problem? Many thanks in advance!

BR  Dingdang

Accepted Solutions
Solution
‎01-30-2015 11:54 AM
Posts: 3,167

## Re: Change observations according to the previous obervation value

lag() or dif();

data have;

infile datalines;

input numbers;

datalines;

1

1

.

2

3

4

.

5

5

6

7

7

;

data want;

set have;

if dif(numbers)=0 then

numbers=.;

run;

All Replies
Solution
‎01-30-2015 11:54 AM
Posts: 3,167

## Re: Change observations according to the previous obervation value

lag() or dif();

data have;

infile datalines;

input numbers;

datalines;

1

1

.

2

3

4

.

5

5

6

7

7

;

data want;

set have;

if dif(numbers)=0 then

numbers=.;

run;

Super User
Posts: 10,778

## Re: Change observations according to the previous obervation value

```data have;
infile datalines;
input numbers;
datalines;
1
1
.
2
3
4
.
5
5
6
7
7
;
run;
data want;
set have;
by numbers notsorted;
if not first.numbers then numbers=.;
run;

data want1;
set have;
by numbers notsorted;
if numbers=lag(numbers) then numbers=.;
run;

```

Xia Keshan

🔒 This topic is solved and locked.