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.
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;
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.