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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1136 views
  • 0 likes
  • 3 in conversation