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: Call for Content

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!

Submit your idea!

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.

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