data want;
set have;
Mili_Year_1=first_year_mili;
Mili_Year_2=first_year_mili +1;
Mili_Year_n=Last_year_mili;
run;
Hello,
I have a wide-form dataset that I'd like to convert to long-form, but the years are inconsistent. I want to create new variables that correspond to the period for each regime and does not extend past the last year by regime. What I have on the left is what I have, and what I want on the right is what I want. I want to do this without listing all the variables. Thank you very much!
country | first_year_mili | last_year_mili | total_years_mili | first_year_civ | last_year_civ | total_years_civ | first_year_int | last_year_int | total_years_int | country | Year | Type | ||
A | 2000 | 2004 | 5 | 2006 | 2010 | 5 | 2005 | 2005 | 1 | A | 2000 | mili | ||
B | 2005 | 2005 | 1 | 2007 | 2012 | 6 | 2006 | 2007 | 2 | A | 2001 | mili | ||
C | 2002 | 2007 | 6 | 2010 | 2012 | 3 | 2009 | 2009 | 1 | A | 2002 | mili | ||
A | 2003 | mili | ||||||||||||
A | 2004 | mili | ||||||||||||
A | 2005 | int | ||||||||||||
A | 2006 | civ | ||||||||||||
A | 2007 | civ | ||||||||||||
A | 2008 | civ | ||||||||||||
A | 2009 | civ | ||||||||||||
A | 2010 | civ | ||||||||||||
B | 2005 | mili | ||||||||||||
B | 2006 | int | ||||||||||||
B | 2007 | int | ||||||||||||
B | 2007 | civ | ||||||||||||
B | 2008 | civ | ||||||||||||
B | 2009 | civ | ||||||||||||
B | 2010 | civ | ||||||||||||
B | 2011 | civ | ||||||||||||
B | 2012 | civ | ||||||||||||
C | 2002 | mili | ||||||||||||
C | 2003 | mili | ||||||||||||
C | 2004 | mili | ||||||||||||
C | 2005 | mili | ||||||||||||
C | 2006 | mili | ||||||||||||
C | 2007 | mili | ||||||||||||
C | 2008 | |||||||||||||
C | 2009 | int | ||||||||||||
C | 2010 | civ | ||||||||||||
C | 2011 | civ | ||||||||||||
C | 2012 | civ | ||||||||||||
data want;
set have;
type = "mili";
do year = first_year_mili to last_year_mili;
output;
end;
type = "int";
do year=first_year_int to last_year_int;
output;
end;
type="civ";
do year=first_year_civ to last_year_civ;
output;
end;
keep country type year;
run;
@michokwu wrote:
data want; set have; Mili_Year_1=first_year_mili; Mili_Year_2=first_year_mili +1; Mili_Year_n=Last_year_mili; run;
Hello,
I have a wide-form dataset that I'd like to convert to long-form, but the years are inconsistent. I want to create new variables that correspond to the period for each regime and does not extend past the last year by regime. What I have on the left is what I have, and what I want on the right is what I want. I want to do this without listing all the variables. Thank you very much!
country first_year_mili last_year_mili total_years_mili first_year_civ last_year_civ total_years_civ first_year_int last_year_int total_years_int country Year Type A 2000 2004 5 2006 2010 5 2005 2005 1 A 2000 mili B 2005 2005 1 2007 2012 6 2006 2007 2 A 2001 mili C 2002 2007 6 2010 2012 3 2009 2009 1 A 2002 mili A 2003 mili A 2004 mili A 2005 int A 2006 civ A 2007 civ A 2008 civ A 2009 civ A 2010 civ B 2005 mili B 2006 int B 2007 int B 2007 civ B 2008 civ B 2009 civ B 2010 civ B 2011 civ B 2012 civ C 2002 mili C 2003 mili C 2004 mili C 2005 mili C 2006 mili C 2007 mili C 2008 C 2009 int C 2010 civ C 2011 civ C 2012 civ
data want;
set have;
type = "mili";
do year = first_year_mili to last_year_mili;
output;
end;
type = "int";
do year=first_year_int to last_year_int;
output;
end;
type="civ";
do year=first_year_civ to last_year_civ;
output;
end;
keep country type year;
run;
@michokwu wrote:
data want; set have; Mili_Year_1=first_year_mili; Mili_Year_2=first_year_mili +1; Mili_Year_n=Last_year_mili; run;
Hello,
I have a wide-form dataset that I'd like to convert to long-form, but the years are inconsistent. I want to create new variables that correspond to the period for each regime and does not extend past the last year by regime. What I have on the left is what I have, and what I want on the right is what I want. I want to do this without listing all the variables. Thank you very much!
country first_year_mili last_year_mili total_years_mili first_year_civ last_year_civ total_years_civ first_year_int last_year_int total_years_int country Year Type A 2000 2004 5 2006 2010 5 2005 2005 1 A 2000 mili B 2005 2005 1 2007 2012 6 2006 2007 2 A 2001 mili C 2002 2007 6 2010 2012 3 2009 2009 1 A 2002 mili A 2003 mili A 2004 mili A 2005 int A 2006 civ A 2007 civ A 2008 civ A 2009 civ A 2010 civ B 2005 mili B 2006 int B 2007 int B 2007 civ B 2008 civ B 2009 civ B 2010 civ B 2011 civ B 2012 civ C 2002 mili C 2003 mili C 2004 mili C 2005 mili C 2006 mili C 2007 mili C 2008 C 2009 int C 2010 civ C 2011 civ C 2012 civ
Thank you so much!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.