Announcement

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

  • Generating lagged variable - string problem

    Hi all,

    I have the following dataset where I want to generate a lagged variable.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double fyear str58 conm double at
    1989 "A.A. IMPORTING CO INC"   10.109
    1989 "AAR CORP"               388.521
    1990 "AAR CORP"               379.958
    1991 "AAR CORP"               395.351
    1992 "AAR CORP"               365.151
    1993 "AAR CORP"               417.626
    1994 "AAR CORP"               425.814
    1995 "AAR CORP"               437.846
    1996 "AAR CORP"               529.584
    1997 "AAR CORP"               670.559
    1998 "AAR CORP"                726.63
    1999 "AAR CORP"               740.998
    2000 "AAR CORP"               701.854
    2001 "AAR CORP"               710.199
    2002 "AAR CORP"               686.621
    2003 "AAR CORP"               709.292
    2004 "AAR CORP"                732.23
    2005 "AAR CORP"               978.819
    2006 "AAR CORP"              1067.633
    2007 "AAR CORP"               1362.01
    end
    The lagged variable I want to create should be the one year lagged variable of "at", e.g. so that in the line of year 1995 for AAR CORP the "at" from year 1994 are shown.

    I already tried the following code which I found here on the forum, but I was reported a string problem. Perhaps you can help me in this regard:

    Code:
    xtset conm fyear, year
    string variables not allowed in varlist;
    conm is a string variable
    r(109);
    
    gen size = l.at
    time variable not set
    r(111);
    Thanks for your replies

    Best

    Matthias

  • #2
    The xtset command tells you that it failed to run because your variable conm is a string variable rather than a numeric variable. You need to use the encode command to create a numeric variable for the xtset and for any other commands, like firm-level indicator variables in your models, that do not support string variables.
    Code:
    . encode conm, generate(nconm)
    
    . xtset nconm fyear, year
           panel variable:  nconm (unbalanced)
            time variable:  fyear, 1989 to 2007
                    delta:  1 year
    
    . gen size = l.at
    (2 missing values generated)
    
    . list, clean
    
           fyear                    conm         at                   nconm       size  
      1.    1989   A.A. IMPORTING CO INC     10.109   A.A. IMPORTING CO INC          .  
      2.    1989                AAR CORP    388.521                AAR CORP          .  
      3.    1990                AAR CORP    379.958                AAR CORP    388.521  
      4.    1991                AAR CORP    395.351                AAR CORP    379.958  
      5.    1992                AAR CORP    365.151                AAR CORP    395.351  
      6.    1993                AAR CORP    417.626                AAR CORP    365.151  
      7.    1994                AAR CORP    425.814                AAR CORP    417.626  
      8.    1995                AAR CORP    437.846                AAR CORP    425.814  
      9.    1996                AAR CORP    529.584                AAR CORP    437.846  
     10.    1997                AAR CORP    670.559                AAR CORP    529.584  
     11.    1998                AAR CORP     726.63                AAR CORP    670.559  
     12.    1999                AAR CORP    740.998                AAR CORP     726.63  
     13.    2000                AAR CORP    701.854                AAR CORP    740.998  
     14.    2001                AAR CORP    710.199                AAR CORP    701.854  
     15.    2002                AAR CORP    686.621                AAR CORP    710.199  
     16.    2003                AAR CORP    709.292                AAR CORP    686.621  
     17.    2004                AAR CORP     732.23                AAR CORP    709.292  
     18.    2005                AAR CORP    978.819                AAR CORP     732.23  
     19.    2006                AAR CORP   1067.633                AAR CORP    978.819  
     20.    2007                AAR CORP    1362.01                AAR CORP   1067.633
    You are concerned that ncomn does not appear to be numeric? That's because it is a numeric variable with value labels that display the original string.
    Code:
     describe nconm
    
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    nconm           long    %21.0g     nconm      
    
    . label list nconm
    nconm:
               1 A.A. IMPORTING CO INC
               2 AAR CORP
    
    . list in 1/5, clean nolabel
    
           fyear                    conm        at   nconm      size  
      1.    1989   A.A. IMPORTING CO INC    10.109       1         .  
      2.    1989                AAR CORP   388.521       2         .  
      3.    1990                AAR CORP   379.958       2   388.521  
      4.    1991                AAR CORP   395.351       2   379.958  
      5.    1992                AAR CORP   365.151       2   395.351

    Comment


    • #3
      Thank you William, that was the code I have been looking for.

      Comment

      Working...
      X