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

I have a column in a table like this - 

Column A

       1

       2

       6

       .

       .

       .

       Now I only want to replace the first missing value in the column with a numeric value(For ex -10)

Output

 

Column A

       1

       2

       6

      10

       .

       .

The code should be dynamic, i.e. It should automatically identify the first missing value and then replace it.

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input a;
datalines;
1
2
6
.
.
.
;

data want;
   set have;
   if a = . & _iorc_ = 0 then do;
      a      = -10;
      _iorc_ = 1;
   end; 
run;

 

Result:

 

a 
1 
2 
6 
-10 
. 
. 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input a;
datalines;
1
2
6
.
.
.
;

data want;
   set have;
   if a = . & _iorc_ = 0 then do;
      a      = -10;
      _iorc_ = 1;
   end; 
run;

 

Result:

 

a 
1 
2 
6 
-10 
. 
. 
s_lassen
Meteorite | Level 14

I would use this construct:

data want;
  set have;
  by a notsorted;
  if first.a and missing(a) then
    a=10;
run;

It is easier to understand than manipulating _IORC_, as suggested by @PeterClemmensen 

EDIT: But it does work differently; it replaces the first of any series of missing values, not just the very first missing value.

So, with you example data, it gives the same result. But with data like this:

data have;
input a;
datalines;
1
2
6
.
.
.
5
.
.
;run;

it gives a different result, as the missing value after 5 is also replaced.

If you only want the first missing value replaced (not the first in each series), another possibility is to modify the data in place, which is probably the fastest, CPU-wise:

data have;
  modify have;
  if missing(a) then do;
    a=10;
    replace;
    stop;
    end;
run;

Meaning that you do not read and copy the 3 zillion observations after the first missing value.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 644 views
  • 0 likes
  • 3 in conversation