Announcement

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

  • creating panel data when the panel variable (ID) has a string format

    Dear,
    I would like to set my data as panel data, but the panel variable (ID) has a string format.
    Here you see the beginning of the dataset. The panel variable goes as follows: "Country abbreviation" + "-" + "numerical values" + "-" + "numerical values"


    Person |
    identifier |
    (fix across |
    modules and |
    waves) | Freq. Percent Cum.
    -------------+-----------------------------------
    AT-030522-01 | 2 0.10 0.10
    AT-054255-02 | 2 0.10 0.19
    AT-060684-01 | 2 0.10 0.29
    AT-070982-01 | 2 0.10 0.38
    AT-070982-02 | 2 0.10 0.48
    .
    .
    .
    ES-019673-02 | 2 0.10 35.44
    ES-027984-02 | 2 0.10 35.54
    ES-035082-01 | 2 0.10 35.63
    ES-045069-01 | 2 0.10 35.73
    ES-093754-01 | 2 0.10 35.82
    ES-108677-01 | 2 0.10 35.92
    .
    .
    .

    What I would like to do is
    1. To replace the country identifier with country codes:
    For the above example, AT (Austria) with 11, ES (Spain) with number 15.

    Click image for larger version

Name:	Screenshot 2022-02-02 at 15.36.19.png
Views:	2
Size:	53.4 KB
ID:	1647917


    2. To delete the two "-"s.

    I've tried solving my problem with the function subinstr() but I was unsuccesful, as I've just started with STATA recently..
    Is there anyone who could help me how to tackle this?

    Attached Files

  • #2
    Rita:
    something in between dream and reality may also work:
    Code:
    et obs 2
    g country_string="AT-030522-01"
    replace country_string="ES-019673-02" in 2
    split country_string , p(-)
    drop country_string2 country_string3
    encode country_string1, g(country_id)
    drop country_string country_string1
    xtset country_idlist
    . list
    
         +----------+
         | countr~d |
         |----------|
      1. |       AT |
      2. |       ES |
         +----------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str12 id str11 country
      "AT-030522-01" "11. Austria"
      "AT-054255-02" "11. Austria"
      "AT-060684-01" "11. Austria"
      "AT-070982-01" "11. Austria"
      "AT-070982-02" "11. Austria"
      "ES-019673-02" "15. Spain"  
      "ES-027984-02" "15. Spain"  
      "ES-035082-01" "15. Spain"  
      "ES-045069-01" "15. Spain"  
      "ES-093754-01" "15. Spain"  
      "ES-108677-01" "15. Spain"  
      end
      
      replace id = substr(country,1,2) + substr(id,3,.)
      replace id = subinstr(id,"-","",.)

      Comment


      • #4
        Thank you Øyvind Snilsberg!

        Comment

        Working...
        X