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
200220076201020123200920091  A2002mili
            A2003mili
            A2004mili
            A2005int
            A2006civ
            A2007civ
            A2008civ
            A2009civ
            A2010civ
            B2005mili
            B2006int
            B2007int
            B2007civ
            B2008civ
            B2009civ
            B2010civ
            B2011civ
            B2012civ
            2002mili
            2003mili
            2004mili
            2005mili
            2006mili
            2007mili
            2008 
            2009int
            2010civ
            2011civ
            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
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
                        2002 mili
                        2003 mili
                        2004 mili
                        2005 mili
                        2006 mili
                        2007 mili
                        2008  
                        2009 int
                        2010 civ
                        2011 civ
                        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
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
                        2002 mili
                        2003 mili
                        2004 mili
                        2005 mili
                        2006 mili
                        2007 mili
                        2008  
                        2009 int
                        2010 civ
                        2011 civ
                        2012 civ
                             

 

michokwu
Quartz | Level 8

Thank you so much!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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