## swaping missing values

Solved
Frequent Contributor
Posts: 79

# swaping missing values

Hi Experts,

data abc;
input sub \$ score;
datalines;
001 120
001 .
002 220
001 .
002 .
003 .
003 230
;
run;
data abc1;
set abc;
score2=lag(score);
if score = . then score=lag(score);
run;

WANT
001 120
001 120
002 220
001 220
002 220
003 220
003 230

thnx

Accepted Solutions
Solution
‎10-07-2016 11:22 AM
Super User
Posts: 10,215

## Re: swaping missing values

Try this:

``````data have;
input sub \$ score;
datalines;
001 120
001 .
002 220
001 .
002 .
003 .
003 230
;
run;

data want;
set have;
retain x1;
if score = .
then score = x1;
else x1 = score;
drop x1;
run;

proc print noobs;
run;``````

Result:

```sub    score

001     120
001     120
002     220
001     220
002     220
003     220
003     230
```
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
New Contributor
Posts: 3

## Re: swaping missing values

Hi Rahul

below can do the trick

data abc1;

retain score;

set abc(rename=(score=score2));

if score2 ne . then score=score2;

drop score2;

run;

Solution
‎10-07-2016 11:22 AM
Super User
Posts: 10,215

## Re: swaping missing values

Try this:

``````data have;
input sub \$ score;
datalines;
001 120
001 .
002 220
001 .
002 .
003 .
003 230
;
run;

data want;
set have;
retain x1;
if score = .
then score = x1;
else x1 = score;
drop x1;
run;

proc print noobs;
run;``````

Result:

```sub    score

001     120
001     120
002     220
001     220
002     220
003     220
003     230
```
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 79

## Re: swaping missing values

Hi KurtBremser,

Thank you for the help.

I have one more point, if the first observation has a missing value then how will it work??

thnx

rahul

☑ This topic is solved.