Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • creating year loop

    I am trying to create a routine that will define year dummies for the time a person is a political leader.

    Using our current President as an example, Obama assumed office in 2009 and is still our current political leader (2009-2016). I want to create the following:

    y2007 y2008 y2009 y2010 y2011...y2016 power1 power2
    Leader y2007 y2008 y2009 y2010 ... y2016 power1 power2
    Obama 0 0 1 1 1 1 2009 2016
    I have the dates correctly formatted as well as the leader structure and am currently using replace with if statements. However, that doesn't seem efficient.

    I think it is more efficient to do this as a macro, but I'm not sure how to correctly define the time between (ie.2009-2016).

    Thanks in advance for any solutions.

  • #2
    Here's an approach that may help.
    Code:
    . input power1 power2
    
            power1     power2
      1. 1001 1003
      2. end
    
    . forvalues year = 1000/1005 {
      2. gen y`year' = inrange(`year',power1,power2)
      3. }
    
    . list, clean noobs
    
        power1   power2   y1000   y1001   y1002   y1003   y1004   y1005  
          1001     1003       0       1       1       1       0       0

    Comment


    • #3
      Thanks for the reply. I am getting an error. Specifically 'invalid syntax'.

      In my data I already have the year created, but do understand the macro you suggest would automatically make the year for me which I recognize as more efficient.

      I pasted a subset of my actual data below as well as the syntax I modified.


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(y2000 y2002 n2_power1 n2_power2)
      0 0 1979 2011
      0 0 1998 2008
      0 0 2006 2011
      0 0 1996 2006
      0 0 1996 2003
      0 0 2003 2006
      0 0 2006 2011
      0 0 1990 2011
      0 0 1997 2011
      0 0 2009 2011
      0 0 1997 2001
      0 0 2001 2011
      0 0 1982 2011
      0 0 1999 2002
      0 0 2002 2002
      0 0 2002 2006
      0 0 2006 2011
      0 0 2011 2011
      0 0 1993 2003
      0 0 1991 2000
      0 0 2000 2001
      0 0 2001 2011
      0 0 1981 2011
      0 0 2011 2011
      0 0 1979 2011
      0 0 1993 2011
      0 0 1991 2011
      0 0 1999 2011
      0 0 1994 2011
      0 0 1967 2009
      0 0 2009 2009
      0 0 2003 2011
      0 0 1981 2001
      0 0 2001 2009
      0 0 2009 2012
      0 0 2010 2011
      0 0 2008 2009
      0 0 1984 2008
      0 0 2000 2011
      0 0 2011 2011
      0 0 1978 2002
      0 0 2002 2011
      end


      Code:
      . forvalues year = y2000/y2012 {
        2.   2. gen y`year' = inrange(`year',n2_power1,n2_power2)
        3.   3. }
      invalid syntax

      Comment


      • #4
        A careful comparison of the forvalues command I posted with your modified version will show you where your syntax differs and is incorrect.

        The error you made on your forvalues command, and your wording in post #1, suggests to me that your background is in SAS and you are attempting to leverage that knowledge into your Stata code. (A Stata macro is something very different than a SAS macro, for example.) I realize you've been using Stata since at least January of this year, your first appearance on Statalist, and you have shown us a number of pieces of reasonably sophisticated code, so the following advice may seem, or actually be, unwarranted. Nevertheless ...

        When I began using Stata in a serious way, coming from a background in SAS, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and manual.

        Stata supplies exceptionally good documentation that amply repays the time spent studying it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

        With that in mind, I particularly commend to your attention Chapter 18 of the Stata User's Guide on Programming Stata to improve your understanding of the code I posted, and to understand how programming Stata differs from programming SAS.

        Again, apologies if this was just a simple oversight on your part.

        Comment


        • #5
          You are correct in that I'm trying to move from SAS to Stata. I now see my error in my logic and have the suggested references stored for future use. Much appreciated!

          Code:
           
           forvalues year = 2000/2011 { gen y`year' = inrange(`year',n2_power1,n2_power2) }

          Comment

          Working...
          X