Announcement

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

  • Automate conditional variable creation

    Hello,

    I would like to automate the creation of categorical variables in Stata. My data look like this
    nom poste2011 rang2011 poste2012 rang2012
    Durand assistant R1 assistant R3
    Lepetit associé R2 associé R2
    Legrand assistant R3 assistant R2
    Gros assistant R1 assistant R3
    Maigre associé R2 associé R2
    Dupuis assistant R3 assistant R2
    I would like to assign to each name (Durand, Lepetit etc.) a state following to two conditions (poste + rang) for each year in a new column.
    For instance assistant + R1 = state A ; assistant + R2 = state B ; associé + R1 = state C etc.
    Can I do this in Stata?
    Thanks for your help!

  • #2
    Your data is in wide layout. See

    Code:
    help reshape
    to get it into long layout. I think you want

    Code:
    egen wanted= group(poste rang), label
    tab wanted

    Comment


    • #3
      Thanks much for your answer. I thought of reshaping the data, but I have the issue that it would mean have a row by name and year? I have no "year" variable, can I extract the "year" variable from the "poste2011" (etc.) variable? Thanks again

      Comment


      • #4
        For your future posts, review FAQ Advice #12 with a focus on how to use the dataex command to provide data examples.

        [CODE]
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str7 nom str9 poste2011 str2 rang2011 str9 poste2012 str2 rang2012
        "Durand"  "assistant" "R1" "assistant" "R3"
        "Lepetit" "associé"  "R2" "associé"  "R2"
        "Legrand" "assistant" "R3" "assistant" "R2"
        "Gros"    "assistant" "R1" "assistant" "R3"
        "Maigre"  "associé"  "R2" "associé"  "R2"
        "Dupuis"  "assistant" "R3" "assistant" "R2"
        end
        
        reshape long poste rang, i(nom) j(year)
        egen wanted= group(poste rang), label
        tab wanted
        Res.:

        Code:
        . tab wanted
        
         group(poste |
               rang) |      Freq.     Percent        Cum.
        -------------+-----------------------------------
        assistant R1 |          2       16.67       16.67
        assistant R2 |          2       16.67       33.33
        assistant R3 |          4       33.33       66.67
          associé R2 |          4       33.33      100.00
        -------------+-----------------------------------
               Total |         12      100.00
        
        . l, sepby(nom)
        
             +--------------------------------------------------+
             |     nom   year       poste   rang         wanted |
             |--------------------------------------------------|
          1. |  Dupuis   2011   assistant     R3   assistant R3 |
          2. |  Dupuis   2012   assistant     R2   assistant R2 |
             |--------------------------------------------------|
          3. |  Durand   2011   assistant     R1   assistant R1 |
          4. |  Durand   2012   assistant     R3   assistant R3 |
             |--------------------------------------------------|
          5. |    Gros   2011   assistant     R1   assistant R1 |
          6. |    Gros   2012   assistant     R3   assistant R3 |
             |--------------------------------------------------|
          7. | Legrand   2011   assistant     R3   assistant R3 |
          8. | Legrand   2012   assistant     R2   assistant R2 |
             |--------------------------------------------------|
          9. | Lepetit   2011     associé     R2     associé R2 |
         10. | Lepetit   2012     associé     R2     associé R2 |
             |--------------------------------------------------|
         11. |  Maigre   2011     associé     R2     associé R2 |
         12. |  Maigre   2012     associé     R2     associé R2 |
             +--------------------------------------------------+
        
        .

        Comment


        • #5
          Hello, many thanks. I did not know dataex, I'll use it next time. A last question: how can I manipulate the label command so that the labels in the new variable look like "etat1"; "etat2", "etat3" etc. Thanks again

          Comment


          • #6
            Drop the label option in the egen command in #4 and see

            Code:
            help label
            on defining your own labels.

            Comment

            Working...
            X