BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michokwu
Quartz | Level 8
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!

countryfirst_year_mililast_year_militotal_years_milifirst_year_civlast_year_civtotal_years_civfirst_year_intlast_year_inttotal_years_int countryYearType
A200020045200620105200520051  A2000mili
B200520051200720126200620072  A2001mili
C 200220076201020123200920091  A2002mili
            A2003mili
            A2004mili
            A2005int
            A2006civ
            A2007civ
            A2008civ
            A2009civ
            A2010civ
            B2005mili
            B2006int
            B2007int
            B2007civ
            B2008civ
            B2009civ
            B2010civ
            B2011civ
            B2012civ
            C 2002mili
            C 2003mili
            C 2004mili
            C 2005mili
            C 2006mili
            C 2007mili
            C 2008 
            C 2009int
            C 2010civ
            C 2011civ
            C 2012civ
               
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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
                             

 

View solution in original post

2 REPLIES 2
Reeza
Super User
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
                             

 

michokwu
Quartz | Level 8

Thank you so much!

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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