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

Hi,

 

my data is something like

 

country       1990       1991        1992       1993   

Burundi        3.01         .                .            0.66   

Canada          .           0.12            .            2.11  

 

and now I want to replace the missing value by the follow non-missing value.

 

my code is

data replace1;
set co2_emission_id;
array [*] '1990'n-'2014'n;
do i=1990 to dim(''n);
if ''n{i}=. then ''n{i}= ''n{i+1};
end;
run;

 

I think there is a name needed between array and [*] so I can modify the do loop.  But my data variable names are numbers so I don't know what I should put there.

 

May I have a suggestion for that?

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

You normally get the answer you need faster if you supply a SAS data step creating a sample data set Have and then show exactly how the desired output should look like if using the data from Have.

options validvarname=any;
data have;
  input country $ '1990'n '1991'n '1992'n '1993'n; 
  datalines;
Burundi 3.01 . . 0.66 
Canada . 0.12 . 2.11 
;
run;

data want;
  set have;
  array n {*} '1990'n-'2014'n;
  do i=1 to dim(n)-1;
    if n[i]=. then
      /* pick the next non-missing value */
      do j=i+1 to dim(n);
        if not missing(n[j]) then 
          do;
            n[i]= n[j];
            leave;
          end;
      end;
  end;
run;

proc print;
run;

 

View solution in original post

8 REPLIES 8
Patrick
Opal | Level 21

@YangYY 

Below code with fixes for all the syntax errors in what you've posted.

data replace1;
  set co2_emission_id;
  array n {*} '1990'n-'2014'n;

  do i=1 to dim(n)-1;
    if n[i]=. then n[i]= n{i+1};
  end;
run;
YangYY
Quartz | Level 8
Thank you for your reply!
The code works but the output is
country 1990 1991 1992 1993

Burundi 3.01 . 0.66 0.66

Canada 0.12 0.12 2.11 2.11

The first missing value would not be replaced. How should I modify my code?

Best wishes
Patrick
Opal | Level 21

You normally get the answer you need faster if you supply a SAS data step creating a sample data set Have and then show exactly how the desired output should look like if using the data from Have.

options validvarname=any;
data have;
  input country $ '1990'n '1991'n '1992'n '1993'n; 
  datalines;
Burundi 3.01 . . 0.66 
Canada . 0.12 . 2.11 
;
run;

data want;
  set have;
  array n {*} '1990'n-'2014'n;
  do i=1 to dim(n)-1;
    if n[i]=. then
      /* pick the next non-missing value */
      do j=i+1 to dim(n);
        if not missing(n[j]) then 
          do;
            n[i]= n[j];
            leave;
          end;
      end;
  end;
run;

proc print;
run;

 

YangYY
Quartz | Level 8
OK and Thank you it works perfect this time!
Patrick
Opal | Level 21

Thanks. I do like the code @Kurt_Bremser posted better though. Here the code with a tweak so it also works with your SAS name literals.

options validvarname=any;

data have;
  input country $ '1990'n '1991'n '1992'n '1993'n;
  datalines;
Burundi 3.01 . . 0.66 
Canada . 0.12 . 2.11 
;

data want;
  set have;
  array n {*} '1990'n-'2014'n;;
  do i = dim(n) - 1 to 1 by -1;
    n{i} = coalesce(n{i},n{i+1});
  end;

  drop i;
run;

proc print;
run;
Kurt_Bremser
Super User
data have;
input country $ _1990 _1991 _1992 _1993;
datalines;   
Burundi 3.01 . . 0.66   
Canada . 0.12 . 2.11
;

data want;
set have;
array n {*} _:;
do i = dim(n) - 1 to 1 by -1;
  n{i} = coalesce(n{i},n{i+1});
end;
drop i;
run;

Note that coding is much easier when one avoids the stupid name literals.

YangYY
Quartz | Level 8
Hi,Thank you for your reply and your code also work perfectly. But since I am new to SAS, the post I accept as solution is easier to understand to me. I just want to let you know your code is awesome and I appreciated for your answer!
Tom
Super User Tom
Super User

The name you use for the array is any valid SAS name just pick one that it not already used by a real variable in your dataset.

 

Normal SAS names start with a letter or underscore, have a maximum length of 32 characters (bytes actually) and contain only letters, digits or underscore.   You might want to avoid names that SAS uses for other things, like functions, but you can still use those names, you just won't be able to call that function since any reference will be assumed to be to the array.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 8 replies
  • 1328 views
  • 6 likes
  • 4 in conversation