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

Hi all,

I'm after some assistance. 

I need to fill in the years in between the observations in the following dataset:

data have;
input Country $ Year Disaster $;
datalines;
Afghanistan 1990 Flood
Afghanistan 1993 Epidemic
...
Afghanistan 2020 Storm
Albania 1992 Landslide
Albania 1994 Storm
...
Albania 2014 Epidemic
...

I would like to have all the observation for each country ranging from year 1990 to 2020, with a value "None" on the variable disaster for the years in which no disaster was registered. For example:

datalines;
Afghanistan 1990 Flood
Afghanistan 1991 None
Afghanistan 1992 None
Afghanistan 1993 Epidemic
...
Afghanistan 2020 Storm
Albania 1990 None
Albania 1991 None
Albania 1992 Landslide
Albania 1993 None
Albania 1994 Storm
...
Albania 2014 Epidemic
...
Albania 2020 None

Any help would be greatly appreciated .

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Create a template having all country/year combinations, then update it with have:

data have;
input Country :$20. Year Disaster :$10.;
datalines;
Afghanistan 1990 Flood
Afghanistan 1993 Epidemic
Afghanistan 2020 Storm
Albania 1992 Landslide
Albania 1994 Storm
Albania 2014 Epidemic
;

data years;
do year = 1990 to 2020;
  output;
end;
run;

proc sql;
create table template as
  select distinct
    have.country,
    years.year,
    "None" as disaster length=10
  from have, years
;
quit;

data want;
update template have;
by country year;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Try this

 

data have;
input Country $1 - 11 Year Disaster :$10.;
datalines;
Afghanistan 1990 Flood     
Afghanistan 1993 Epidemic  
Afghanistan 2020 Storm     
Albania     1992 Landslide 
Albania     1994 Storm     
Albania     2014 Epidemic  
;

data want;

   if _N_ = 1 then do;
      dcl hash h(dataset : 'have');
      h.definekey('Country', 'Year');
      h.definedata('Disaster');
      h.definedone();
   end;

   set have;
   by Country;

   if first.Country then do;
      do Year = 1990 to 2020;
         if h.find() then Disaster = 'None';
         output;
      end;
   end;
   
run;
Kurt_Bremser
Super User

Create a template having all country/year combinations, then update it with have:

data have;
input Country :$20. Year Disaster :$10.;
datalines;
Afghanistan 1990 Flood
Afghanistan 1993 Epidemic
Afghanistan 2020 Storm
Albania 1992 Landslide
Albania 1994 Storm
Albania 2014 Epidemic
;

data years;
do year = 1990 to 2020;
  output;
end;
run;

proc sql;
create table template as
  select distinct
    have.country,
    years.year,
    "None" as disaster length=10
  from have, years
;
quit;

data want;
update template have;
by country year;
run;
mkeintz
PROC Star

This is a case where use of a temporary array of disasters, indexed by year, and defaulting to 'None" is a nice solution:

 

data have;
  input Country $ Year Disaster :$9.;
datalines;
Afghanistan 1990 Flood
Afghanistan 1993 Epidemic
Afghanistan 2020 Storm
Albania 1992 Landslide
Albania 1994 Storm
run;

data want;
  set have;
  by country;
  array dhist {1990:2020} $9 _temporary_ (31*'None');
  dhist{year}=disaster;
  if last.country then do year=1990 to 2020;
    disaster=dhist{year};
    output;
    dhist{year}='None';
  end;
run;

The _temporary_ array will have its values (i.e. mostly a series of 'None') retained from one observation to the next.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 576 views
  • 7 likes
  • 4 in conversation