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
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;
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;
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;
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;
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.
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.
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!
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.