Help using Base SAS procedures

Change observations according to the previous obervation value

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

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
Respected Advisor
Posts: 3,124

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;

View solution in original post


All Replies
Solution
‎01-30-2015 11:54 AM
Respected Advisor
Posts: 3,124

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: 9,682

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 202 views
  • 4 likes
  • 3 in conversation