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.

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 896 views
  • 0 likes
  • 2 in conversation