BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BruceBrad
Lapis Lazuli | Level 10

I have a dataset with observations for each year for each person. Some years are missing. I want to expand the file to have all years for each person (with missing values for the missing years). Can this be done with an SQL statement. The code below doesn't work.

 

data one;
input id year var;
cards;
1 2001 3
1 2002 5
1 2003 7
2 2002 4
2 2003 6
;
run;

data years;
input year;
cards;
2001
2002
2003
;
run;

data desired;
input id year var;
cards;
1 2001 3
1 2002 5
1 2003 7
2 2001 .
2 2002 4
2 2003 6
;

/* this doesn't add in the missing row */;
proc sql;
  create table comb as
  select years.year, one.id, one.var from
  years left join one
  on one.year = years.year
  order by id,year ;
quit;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20

One way is to make a cartesian product between id and years firs, then do the left join from your original table.

proc sql;
  create table comb as
  select id_years.year, id_years.id, one.var from
  (select id, years.year 
	from (select distinct id from one), years) id_years
left join one
  on one.id = id_years.id and 
	one.year = id_years.year
  order by id,year ;
quit;
Data never sleeps

View solution in original post

2 REPLIES 2
LinusH
Tourmaline | Level 20

One way is to make a cartesian product between id and years firs, then do the left join from your original table.

proc sql;
  create table comb as
  select id_years.year, id_years.id, one.var from
  (select id, years.year 
	from (select distinct id from one), years) id_years
left join one
  on one.id = id_years.id and 
	one.year = id_years.year
  order by id,year ;
quit;
Data never sleeps
BruceBrad
Lapis Lazuli | Level 10
Thanks. Very helpful.

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!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 469 views
  • 0 likes
  • 2 in conversation