DATA Step, Macro, Functions and more

Remplace missing value

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Remplace missing value

[ Edited ]

hello,

 

I have a large data set, look like this :

 

data mydata;
input id month mark last;
cards;
1 4 14.5 28
1 1 . .
1 3 . .
2 7 . .
;
run;

 

and I want to remplace all missing value by 14.5 and 28, I mean like this

ID month mark   last
1   4        14.5    28
1   5        14.5    28
1   1        14.5    28
1   3        14.5    28
2   7        14.5    28

 

Thank you


Accepted Solutions
Solution
‎04-06-2017 04:44 AM
PROC Star
Posts: 549

Re: Remplace missing value

ok, with automatically, I take it that you mean that you always have the nonmissing value in the first observation? Smiley Happy

 

So

 

data want;
	set mydata;
	if _N_ = 1 then do;
		temp_mark = mark;
		temp_last = last;
		retain temp_mark temp_last;
	end;
	if missing(mark) then mark=temp_mark;
	if missing(last) then last = temp_last;

	drop temp_mark temp_last;
run;

View solution in original post


All Replies
PROC Star
Posts: 549

Re: Remplace missing value

Like this?

 

data mydata;
input id month mark last;
cards;
1 4 14.5 28
1 1 . .
1 3 . .
2 7 . .
;
run;

data want;
	set mydata;
	if missing(mark) then mark=14.5;
	if missing(last) then last = 28;
run;
Contributor
Posts: 36

Re: Remplace missing value

Yes but since I have a large data set, I want to do it automatically

Solution
‎04-06-2017 04:44 AM
PROC Star
Posts: 549

Re: Remplace missing value

ok, with automatically, I take it that you mean that you always have the nonmissing value in the first observation? Smiley Happy

 

So

 

data want;
	set mydata;
	if _N_ = 1 then do;
		temp_mark = mark;
		temp_last = last;
		retain temp_mark temp_last;
	end;
	if missing(mark) then mark=temp_mark;
	if missing(last) then last = temp_last;

	drop temp_mark temp_last;
run;
Contributor
Posts: 36

Re: Remplace missing value

Yes Smiley Happy

Super User
Posts: 9,671

Re: Remplace missing value

data mydata;
input id month mark last;
cards;
1 4 14.5 28
1 1 . .
1 3 . .
2 7 . .
;
run;

data want;
	set mydata(drop= mark last);
 if _n_=1 then set mydata(keep= mark last obs=1);
run;
☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 131 views
  • 2 likes
  • 3 in conversation