Announcement

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

  • Recode variables

    Good day everyone,

    I'd like to categorize these variables into 3 or 4 options e.g Father and Mother = Parent (s), Brother and Sister = siblings(s)

    clear
    input str31 Familycomposition
    "Father, Mother, Sister"
    "Father, Mother, Sister, Brother"
    "Father, Mother"
    "Father, Mother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Sister"
    "Father, Mother"
    "Father, Mother"
    "Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Mother, Sister"
    "Father, Mother, Brother"
    "Father, Mother"
    "Father, Mother, Sister"
    "Brother"
    "Father, Mother, Sister"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Sister"
    "Father, Mother"
    "Father, Mother, Sister"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father"
    "Father, Mother"
    "Father, Mother, Brother"
    "Father, Mother"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Mother"
    "Father, Mother, Sister"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Sister"
    "Father, Mother, Sister"
    "Father, Mother, Sister"
    "Father"
    "Father, Mother, Brother"
    "Father, Mother, Sister"
    "Father, Mother, Sister, Brother"
    "Father, Mother"
    "Father, Mother, Sister"
    "Father, Mother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Brother"
    "Father, Mother, Sister, Brother"
    "Father, Mother, Brother"
    "Father, Mother"
    "Brother"
    "Father, Mother, Brother"
    "Father, Mother"
    end

  • #2
    I'm not clear what it is that you want. Do you want to create binary variables, say one for Parents, another one for Siblings, and then code them depending on whether Familycomposition contains anyone who would fall in those categories? Or are you looking to create a single variable with various codes for the composition? If the latter, what is the list of mutually exclusive and exhaustive categories you would like to code?

    Here is some code that does both of the above:

    Code:
    gen byte siblings = strpos(Familycomposition, "Sister") | strpos(Familycomposition, "Brother")
    gen byte parents = strpos(Familycomposition, "Mother") | strpos(Familycomposition, "Father")
    
    gen byte composition = cond(siblings & parents, 1, ///
                            cond(siblings & !parents, 2, ///
                             cond(!siblings & parents, 3, .)))
    label define COMPOSITION 1 "Siblings and Parents" 2 "Only siblings" 3 "Only parents"
    label values composition COMPOSITION
    Last edited by Hemanshu Kumar; 20 Dec 2022, 07:40.

    Comment


    • #3
      Thanks for the help. yes, i'd like to create binary variables and code them based on their categories

      Comment


      • #4
        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str31 Familycomposition
        "Father, Mother, Sister"         
        "Father, Mother, Sister, Brother"
        "Father, Mother"                 
        "Father, Mother, Brother"        
        "Mother, Brother"                
        "Mother, Sister"                 
        "Brother"                        
        "Father, Brother"                
        "Father"                         
        "Mother"                         
        "Sister"                         
        end
        
        foreach v in Father Mother Sister Brother {
            gen `v' = strpos(Familycomposition, "`v'") > 0 
        }
        
        gen Sibling = Sister | Brother

        Comment


        • #5
          Thank you.. there seem to be a problem. my total variable is 373 and I applied your code to them but it keeps freezing. it probably worked because I didn't get an error message but I can't do anything with it

          Comment


          • #6
            First, some terminology: "variables" are the columns in your dataset; they have names. In your example data, there was one variable, called Familycomposition. The rows in your data are called "observations". Do you have 373 observations, or 373 variables?

            If it is 373 observations, the code should run almost instantaneously. If 373 variables over which you are looping, and potentially many millions of observations, it could be that the code just needs some time to run.

            So also: what exactly do you mean by "it keeps freezing"? You say you don't get an error message. Are you needing to force Stata to exit?

            Can you show us the complete code you are running?

            Comment


            • #7
              I have 373 observations. the code ran instantaneously without any error but I can't run any analysis or view my data set.

              I ran this code but with all my 373 observations
              clear input str31 Familycomposition
              "Father, Mother, Sister"
              "Father, Mother, Sister, Brother"
              "Father, Mother"
              "Father, Mother, Brother"
              "Mother, Brother"
              "Mother, Sister" "Brother"
              "Father, Brother" "Father"
              "Mother" "Sister"
              end

              foreach v in Father Mother Sister Brother {gen `v' = strpos(Familycomposition, "`v'") > 0 }
              gen Sibling = Sister | Brother
              Last edited by Kehinde Akinlagun; 20 Dec 2022, 23:51.

              Comment


              • #8
                The code in #4 works for me.. Pay attention to line breaks. > is HTML for > :Stata wants to see > (greater than).

                Comment


                • #9
                  It worked. Thank you

                  Comment

                  Working...
                  X