BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Rahul_SAS
Quartz | Level 8

Hi Experts,
I need to swap missing value with above value so please help me in the same.

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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 

View solution in original post

3 REPLIES 3
Tom_C_Mortensen
Calcite | Level 5

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;

Kurt_Bremser
Super User

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 
Rahul_SAS
Quartz | Level 8

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1021 views
  • 0 likes
  • 3 in conversation