Announcement

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

  • Problem with lag and lead operators in xtset

    I have a problem with the F. command in xtset. The relevant part of the dataset is:

    Code:
    . xtset authid year
    . d authid year afid countrycode
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    authid          double  %15.0g                Scopus identifier of author
    year            float   %9.0g                
    afid            long    %15.0g                Scopus identifier of affiliation.
    countrycode     int     %52.0g     etich      country-code
    For a given panel, I get:

    Code:
    . list year  authid afid* countrycode* if authid == 7003989689
    
             +-----------------------------------------------+
             | year       authid       afid      countrycode |
             |-----------------------------------------------|
     549793. | 1988   7003989689   60016418   United Kingdom |
     549794. | 1989   7003989689   60033125   United Kingdom |
     549795. | 1990   7003989689   60033125   United Kingdom |
     549796. | 1991   7003989689   60033125   United Kingdom |
     549797. | 1992   7003989689   60033125   United Kingdom |
             |-----------------------------------------------|
     549798. | 1993   7003989689   60033125   United Kingdom |
     549799. | 1994   7003989689   60033125   United Kingdom |
     549800. | 1995   7003989689   60015138   United Kingdom |
     549801. | 1996   7003989689   60015138   United Kingdom |
     549802. | 1997   7003989689   60015138   United Kingdom |
             |-----------------------------------------------|
     549803. | 1998   7003989689   60015138   United Kingdom |
     549804. | 1999   7003989689   60027509            Italy |
             +-----------------------------------------------+
    I want to generate a variable containing the next year's countrycode and authid. The commands

    Code:
    . gen countrycode_next = F.countrycode
    (2,901,342 missing values generated)
    
    . gen afid_next = F.afid
    (2,901,342 missing values generated)
    give the following outcome:

    Code:
    . list year  authid afid* countrycode* if authid == 7003989689
     
             +-------------------------------------------------------------------------------------------------------------------------------+
             | year       authid       afid   afid_n~t   afid_s~o      countrycode   countrycode_~t |
             |--------------------------------------------------------------------------------------------------------------------------|
     549793. | 1988   7003989689   60016418   60033124          .   United Kingdom   United Kingdom |
     549794. | 1989   7003989689   60033125   60033124          .   United Kingdom   United Kingdom |
     549795. | 1990   7003989689   60033125   60033124          .   United Kingdom   United Kingdom |
     549796. | 1991   7003989689   60033125   60033124          .   United Kingdom   United Kingdom |
     549797. | 1992   7003989689   60033125   60033124          .   United Kingdom   United Kingdom |
             |---------------------------------------------------------------------------------------------------------------------|
     549798. | 1993   7003989689   60033125   60033124          .   United Kingdom   United Kingdom |
     549799. | 1994   7003989689   60033125   60015136          .   United Kingdom   United Kingdom |
     549800. | 1995   7003989689   60015138   60015136          .   United Kingdom   United Kingdom |
     549801. | 1996   7003989689   60015138   60015136          .   United Kingdom   United Kingdom |
     549802. | 1997   7003989689   60015138   60015136          .   United Kingdom   United Kingdom |
             |----------------------------------------------------------------------------------------------------------------|
     549803. | 1998   7003989689   60015138   60027508          .   United Kingdom            Italy |
     549804. | 1999   7003989689   60027509          .          .            Italy                . |
             +-----------------------------------------------------------------------------------------------------+
    While the countrycode is correct, the afid is reduced by 1 or 2. I cannot think of an error which would determine this behaviour, and any help would be much appreciated (PS this is my first post, so apologies if I have inadvertently missed some convention).

  • #2
    See

    Code:
    help precision
    The default storage type for numeric variables in Stata is float, and your numbers are too big for this. See, e.g.,

    Code:
    di float(60016418)
    Res.:

    Code:
    . di float(60016418)
    60016416
    So you want to hold the generated variable in double precision.

    Code:
    gen double afid_next = F.afid

    Comment


    • #3
      Also, looking at post #1, we see that afid was stored as a long variable, which should have served as a hint that F.afid should at least be stored as a long variable. But as the table below tells us, a double has more precision than a long, so nothing is lost by storing it in a long.

      Here are the limits on storage of decimal integers with full accuracy in the various numeric storage types. The fixed-point variables lose the 27 largest positive values to missing value codes; the similar loss for floating point variables occurs only for the largest exponent, so it doesn't affect the much smaller integer values.


      byte - 7 bits -127 100
      int - 15 bits -32,767 32,740
      long - 31 bits -2,147,483,647 2,147,483,620
      float - 24 bits -16,777,216 16,777,216
      double - 53 bits -9,007,199,254,740,992 9,007,199,254,740,992


      Comment


      • #4
        Thank you both very much. It works a treat, and I can see the reason for the error.

        Comment

        Working...
        X