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

I have a dataset that looks like this 

 

Have:

ID First Year Last Year Name Country
1 2015 2020 ABC USA
2 1990 2000 XYZ Canada
3 2002 2005 HIJ UK

 

I want the observations to be duplicated from the first year till last year and there will be only one colum for year. Such as 

 

Want:

ID Year Name Country
1 2015 ABC USA
1 2016 ABC USA
1 2017 ABC USA
1 2018 ABC USA
1 2019 ABC USA
1 2020 ABC USA
2 1990 XYZ Canada
2 1991 XYZ Canada
2 1992 XYZ Canada
2 1993 XYZ Canada
2 1994 XYZ Canada
2 1995 XYZ Canada
2 1996 XYZ Canada
2 1997 XYZ Canada
2 1998 XYZ Canada
2 1999 XYZ Canada
2 2000 XYZ Canada
3 2002 HIJ UK
3 2003 HIJ UK
3 2004 HIJ UK
3 2005 HIJ UK

 

I think it needs the DO Statement but can't seem to figure out the exact code for it. Would really appreciate if anyone can help. 

1 ACCEPTED SOLUTION

Accepted Solutions
japelin
Rhodochrosite | Level 12

How about this code?

data have;
  input ID FirstYear LastYear Name $ Country $;
datalines;
1 2015 2020 ABC USA
2 1990 2000 XYZ Canada
3 2002 2005 HIJ UK
;
run;

data want;
  set have;
  do year=firstyear to lastyear;
    output;
  end;
  drop firstyear lastyear;
run;

View solution in original post

2 REPLIES 2
japelin
Rhodochrosite | Level 12

How about this code?

data have;
  input ID FirstYear LastYear Name $ Country $;
datalines;
1 2015 2020 ABC USA
2 1990 2000 XYZ Canada
3 2002 2005 HIJ UK
;
run;

data want;
  set have;
  do year=firstyear to lastyear;
    output;
  end;
  drop firstyear lastyear;
run;
Adubhai
Obsidian | Level 7
Thanks a lot! This worked perfectly

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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