Announcement

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

  • generating a new variable for exporting

    Hello dear all,

    I have a question for generating e new variable exporting. So i want to know the first year in which this firm begin to export, and the year the firm exit form exporting.how can I do? My panel data is unbalanced so may be not all the firm report for these data.. my dataset contains data from 2003-2012. bvdid is the identifier for each firm. export is a dummy var (1 exp; 0 non-exp)
    I was thinking of this solution:

    bys bvdid: egen minyear=min(year)
    bys bvdid: egen maxyear=max(year)
    gen entry=0
    replace entry=1 if minyear>2003 & year==minyear & export==1

    gen exit=0
    replace exit=1 if maxyear<2012 & year==maxyear & export==1

    what do you think is it correct?

    thank a lot

  • #2
    Only the first two lines of your code pay any attention to panel structure. For that and other reasons you need something different.

    As I understand it, you just need something like

    Code:
    bysort bvdid : egen minyear = min(year / export) 
    
    bysort bvdid : egen minyear = min(year / (export == 1))
    I give two equivalent solutions here because people often find the first a little tricky. Even the second can seem a little tricky. But consider the ratio

    Code:
    year / export
    When export is 1 this is just identical to year. When export is 0, Stata returns missing for year / export given the division by zero. Here that is useful! Hence the minimum in any panel will be the first year when export is 1, unless that never happens, in which case missing will be returned.

    See http://www.stata-journal.com/sjpdf.h...iclenum=dm0055 for discussion of this and related devices.

    Comment


    • #3
      Thank you Nick so so much.. really it is a relief that you replied to me..

      Comment


      • #4
        Hello again,
        My question today regards export as well. I want to generate a new variable which should tell me that a firm STARTED or STOPPED exporting. I have an unbalanced panel data, 2003-2012.

        My idea is:
        tsset firm year

        gen start=1 if l.export==0 & export==1
        gen exit=1 if l.export==1 & export==0

        This works fine if the firm is not exporting from the first year (2003). But if the firm begins to export in 2003 the variable start does not get a value of 1. And the problem persist with the exit variable.
        Do you have any suggestion?

        Comment


        • #5
          Please use CODE delimiters to show code. This is explained in the FAQ Advice.

          All depends on your definitions. On one strict reading, you can't say that a value of 1 or 0 for the first known year is either a start or a stop, respectively, unless you know the previous value, and you don't.

          If you are happy with start as meaning the value of export is 1 and there is no known previous value then

          Code:
          gen start = inlist(l.export, 0, .)  & export
          will catch the start of a panel too (for which l.export is missing). Presumably the logic for stopping is the same, but with 0 and 1 exchanged.

          Comment


          • #6
            I am trying from yesterday with this solution but is still not working..I have an unbalanced panel data from 2003-2012. There are cases where the firm exports for two years in a row and then stops, and then the next year exports again. So for the same firm I get more values for start. I just need the first year when the firm begins to export.. do you have any other suggestion for me please?
            Thanks
            K

            Comment


            • #7
              Use dataex (SSC) to show an example of what you have; show your exact code that you are using; and show us what you want.

              Comment


              • #8
                Here is the code:

                Code:
                clear
                input int year str13 bvdid float(export start) double exportrevenue byte firmtag float firm long numberofemployees byte foreign float(employees lntfp)
                2003 "HR1371746" 0 0 0 0 28104 7 0 1.7917595 10.916712
                2005 "HR1371746" 1 1 208736.32 0 28104 7 0 1.9459101 10.689961
                2007 "HR1371746" 1 1 225662.43 0 28104 10 0 1.9459101 10.640684
                2010 "HR1371746" 1 1 72970 0 28104 7 0 1.9459101 11.197152
                2011 "HR1371746" 1 0 45243.616 1 28104 6 0 0 11.276681
                2003 "HR1371908" 0 0 0 0 28105 9 0 2.7080503 11.258618
                2004 "HR1371908" 0 0 0 1 28105 11 0 2.3025851 10.839351
                2005 "HR1371908" 0 0 0 0 28105 10 0 1.7917595 11.058489
                2006 "HR1371908" 0 0 0 0 28105 15 0 2.7080503 11.303202
                2007 "HR1371908" 0 0 0 0 28105 15 0 2.3025851 11.016809
                2010 "HR1371908" 0 0 0 0 28105 10 0 2.1972246 10.593199
                2011 "HR1371908" 0 0 0 0 28105 6 0 2.3978953 11.341243
                2003 "HR1371924" 1 1 582535.6 1 28106 3 1 2.3025851 13.232493
                2005 "HR1371924" 1 1 1901160.9 0 28106 6 1 2.6390574 12.926038
                2006 "HR1371924" 1 0 2337855.5 0 28106 9 1 2.3978953 12.880394
                2007 "HR1371924" 1 0 4686329.4 0 28106 12 1 2.1972246 12.783858
                2008 "HR1371924" 1 0 6633676.3 0 28106 14 1 2.4849067 12.68496
                2009 "HR1371924" 1 0 3976796.7 0 28106 12 1 1.0986123 12.504166
                2010 "HR1371924" 1 0 3017300 0 28106 11 1 2.4849067 12.55765
                2011 "HR1371924" 1 0 3924224.7 0 28106 12 1 2.4849067 12.838983
                2012 "HR1371924" 1 0 2201168.3 0 28106 12 1 2.4849067 12.544183
                2003 "HR1371983" 1 1 480782.65 1 28108 1 0 1.7917595 10.830196
                end
                label def cat_export 0 "Non-exporting", modify
                label def cat_export 1 "Exporting", modify
                label values export cat_export
                label def foreign1 0 "Domestic firm", modify
                label def foreign1 1 "Foreign firm", modify
                label values foreign foreign1
                I would like to create the value start and exit for my unbalanced panel data in order to regress these variables with TFP of firms.

                Comment


                • #9
                  Thanks. That makes things clearer. But there is no variable exit here, nor any indication of what you tried to use to create it and why that is not what you want.

                  Comment


                  • #10
                    Thanks to you that are so kind to me and answering.. And i am sorry for not using the dataex before.

                    here is the code with the start and stop variables.

                    Code:
                    clear
                    input str13 bvdid int year double exportrevenue float(export start stop lntfp)
                    "HR1371746" 2003 0 0 0 1 10.916712
                    "HR1371746" 2005 208736.32 1 1 0 10.689961
                    "HR1371746" 2007 225662.43 1 1 0 10.640684
                    "HR1371746" 2010 72970 1 1 0 11.197152
                    "HR1371746" 2011 45243.616 1 0 0 11.276681
                    "HR1371908" 2003 0 0 0 1 11.258618
                    "HR1371908" 2004 0 0 0 0 10.839351
                    "HR1371908" 2005 0 0 0 0 11.058489
                    "HR1371908" 2006 0 0 0 0 11.303202
                    "HR1371908" 2007 0 0 0 0 11.016809
                    "HR1371908" 2010 0 0 0 1 10.593199
                    "HR1371908" 2011 0 0 0 0 11.341243
                    "HR1371924" 2003 582535.6 1 1 0 13.232493
                    "HR1371924" 2005 1901160.9 1 1 0 12.926038
                    "HR1371924" 2006 2337855.5 1 0 0 12.880394
                    "HR1371924" 2007 4686329.4 1 0 0 12.783858
                    "HR1371924" 2008 6633676.3 1 0 0 12.68496
                    "HR1371924" 2009 3976796.7 1 0 0 12.504166
                    "HR1371924" 2010 3017300 1 0 0 12.55765
                    "HR1371924" 2011 3924224.7 1 0 0 12.838983
                    "HR1371924" 2012 2201168.3 1 0 0 12.544183
                    "HR1371983" 2003 480782.65 1 1 0 10.830196
                    end
                    label def cat_export 0 "Non-exporting", modify
                    label def cat_export 1 "Exporting", modify
                    label values export cat_export
                    I actually want the variable start to take value 1 only for the first year that the firm is exporting. And I want the variable stop to take value 1 only the last year the firm is reporting export (independently if the firm is exporting or not other years) . Basically i want to know the first time the firm entered the export market, and the year she exited from the exporting market.


                    for example for firm HR 1371746 i want that start takes value 1 only in 2005 ( first year she is reporting exports) and the other years should be 0. and for the stop variable i want to take value 1 only in 2011 and the other years should be 0.

                    I really hope that my question is clear..

                    Comment


                    • #11
                      If you order observations by firm and then by whether or not they export, then what you are seeking is simply to identify the first and last observation of the group of observations who export. Something like

                      Code:
                      bysort bvdid export: gen start = _n == 1 & export == "Exporting":cat_export 
                      bysort bvdid export: gen stop = _n == _N & export == "Exporting":cat_export
                      With encoded/labelled variables, it's easy to make a mistake as to the value label used. Is exporting coded as 0 or 1? The "Exporting":cat_export construct will expand to the correct numerical value.

                      Comment


                      • #12
                        Thanks Robert very much,

                        maybe it's my fault.. I really maybe don't explain myself and what I want to do properly..But is not working at all..

                        here is the code that i get after writing the code you suggested


                        Code:
                        clear
                        input str13 bvdid int year double exportrevenue float(export start)
                        "HR1371746" 2003 0 0 0
                        "HR1371746" 2007 225662.43 1 1
                        "HR1371746" 2010 72970 1 0
                        "HR1371746" 2011 45243.616 1 0
                        "HR1371746" 2005 208736.32 1 0
                        "HR1371908" 2010 0 0 0
                        "HR1371908" 2011 0 0 0
                        "HR1371908" 2006 0 0 0
                        "HR1371908" 2007 0 0 0
                        "HR1371908" 2004 0 0 0
                        "HR1371908" 2005 0 0 0
                        "HR1371908" 2003 0 0 0
                        "HR1371924" 2006 2337855.5 1 1
                        "HR1371924" 2009 3976796.7 1 0
                        "HR1371924" 2010 3017300 1 0
                        "HR1371924" 2007 4686329.4 1 0
                        "HR1371924" 2008 6633676.3 1 0
                        "HR1371924" 2005 1901160.9 1 0
                        "HR1371924" 2003 582535.6 1 0
                        "HR1371924" 2011 3924224.7 1 0
                        "HR1371924" 2012 2201168.3 1 0
                        "HR1371983" 2003 480782.65 1 1
                        end
                        label def cat_export 0 "Non-exporting", modify
                        label def cat_export 1 "Exporting", modify
                        label values export cat_export


                        As you can see for firm HR1371746 start takes value 1 in year2007. In fact start should be one in year2005. because 2005 is the first year the firm is reporting..And for the stop variable I get the same mistake. stop take value 1 in year2005; which is not what i want.. stop should take value1 in year 2011(last reporting year for exporting)

                        I tried to insert in your code year as well like this (bysort bvdid year export), but is still a mismatch..wrong everywhere.

                        If I really don't fix this, does it mean that i have some problems with my data? that something wrong there?

                        Comment


                        • #13
                          Sorry for editing this post. I did not notice the problem in my initial reply. Here's a revised version that I hope will work for you. My original suggestion in #11 forgot to sort by year within the bvdid export group. My bad.

                          Code:
                          clear
                          input str13 bvdid int year double exportrevenue float(export start stop lntfp)
                          "HR1371746" 2003 0 0 0 1 10.916712
                          "HR1371746" 2005 208736.32 1 1 0 10.689961
                          "HR1371746" 2007 225662.43 1 1 0 10.640684
                          "HR1371746" 2010 72970 1 1 0 11.197152
                          "HR1371746" 2011 45243.616 1 0 0 11.276681
                          "HR1371908" 2003 0 0 0 1 11.258618
                          "HR1371908" 2004 0 0 0 0 10.839351
                          "HR1371908" 2005 0 0 0 0 11.058489
                          "HR1371908" 2006 0 0 0 0 11.303202
                          "HR1371908" 2007 0 0 0 0 11.016809
                          "HR1371908" 2010 0 0 0 1 10.593199
                          "HR1371908" 2011 0 0 0 0 11.341243
                          "HR1371924" 2003 582535.6 1 1 0 13.232493
                          "HR1371924" 2005 1901160.9 1 1 0 12.926038
                          "HR1371924" 2006 2337855.5 1 0 0 12.880394
                          "HR1371924" 2007 4686329.4 1 0 0 12.783858
                          "HR1371924" 2008 6633676.3 1 0 0 12.68496
                          "HR1371924" 2009 3976796.7 1 0 0 12.504166
                          "HR1371924" 2010 3017300 1 0 0 12.55765
                          "HR1371924" 2011 3924224.7 1 0 0 12.838983
                          "HR1371924" 2012 2201168.3 1 0 0 12.544183
                          "HR1371983" 2003 480782.65 1 1 0 10.830196
                          end
                          label def cat_export 0 "Non-exporting", modify
                          label def cat_export 1 "Exporting", modify
                          label values export cat_export
                          
                          drop start stop
                          bysort bvdid export (year): gen start = _n == 1 & export == "Exporting":cat_export
                          bysort bvdid export (year): gen stop = _n == _N & export == "Exporting":cat_export
                          and the results for "HR1371746"

                          Code:
                          * Example generated by -dataex-. To install: ssc install dataex
                          clear
                          input str13 bvdid int year double exportrevenue float(export lntfp start stop)
                          "HR1371746" 2003         0 0 10.916712 0 0
                          "HR1371746" 2005 208736.32 1 10.689961 1 0
                          "HR1371746" 2007 225662.43 1 10.640684 0 0
                          "HR1371746" 2010     72970 1 11.197152 0 0
                          "HR1371746" 2011 45243.616 1  11.27668 0 1
                          end
                          label values export cat_export
                          label def cat_export 0 "Non-exporting", modify
                          label def cat_export 1 "Exporting", modify
                          Last edited by Robert Picard; 30 Sep 2015, 11:04. Reason: apologies for changing the post, initial version was wrong

                          Comment


                          • #14
                            Robert I really didn't believe that I could solve this issue.. it seems to me very difficult.. Actually you solved it.. it's your credit! Thank you again.. you made my day!

                            Comment


                            • #15
                              Glad that this solved the problem for you. After thinking about it a bit more, here's a revised version that orders the data beforehand and checks that there are not multiple observations per bvdid export year group. This is a safer way to proceed when flagging the first or last observation within a group (and would have caught my mistake).

                              Code:
                              isid bvdid export year, sort
                              by bvdid export: gen start = _n == 1 & export == "Exporting":cat_export 
                              by bvdid export: gen stop = _n == _N & export == "Exporting":cat_export

                              Comment

                              Working...
                              X