Announcement

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

  • Generating a new variable (gen)

    Hello friends,

    I am attempting to generate and regroup a variable "jbstat" into three new variables (employed, unemployed, and neither). Unfortunately, the values do not fall consecutively, so I cannot recode through the greater than, less than or equal to function. Any help will be greatly appreciated (I have attached the code I have written below - the green text represents the labels that belong in each section)!

    Regards,
    Gabi



  • #2
    Hi Gabi, I may be missing something, but I am not seeing any attachment. can you please provide a data example using -dataex-? It also helps if you include your code in the body of a comment rather than an attached document. Remember to surround it in CODE tags.

    Comment


    • #3
      Expanding on Daniel's advice, to present code and results readably, please copy them from the Results window and paste them into a code block in the Forum editor using code delimiters [CODE] and [/CODE], as explained in the Statalist FAQ linked to at the top of the page. For example, the following:

      [CODE]
      // sample code
      sysuse auto, clear
      describe
      [/CODE]

      will be presented in the post as the following:
      Code:
      // sample code
      sysuse auto, clear
      describe

      Comment


      • #4
        gen employed = .
        replace employed = 1 if jbstat>= & jbstat<
        replace employed = 0 if jbstat< | jbstat>=



        gen unemployed = .
        replace unemployed = 1 if jbstat>=3 & jbstat<4
        replace unemployed = 0 if jbstat<3 | jbstat>=4


        gen neither = .
        replace neither = 1 if jbstat>= & jbstat<
        replace neither = 0 if jbstat< | jbstat>=


        *EMPLOYED/// 1, 2, 5, 6, 9, 10, 11
        *UNEMPLOYED/// 3
        *NEITHER/// 4, 7, 8, 97

        Comment


        • #5
          Hi Gabi,

          If I am interpreting this correctly, I believe you want something like this:

          Code:
          gen employed = 0
          replace employed = 1 if jbstat == 1 | jbstat == 2 | jbstat == 5 | jbstat == 6 | jbstat == 9 | jbstat == 10 | jbstat == 11
          
          gen unemployed = 0
          replace unemployed = 1 if jbstat == 3
          
          gen neither = 0
          replace neither = 1 if jbstat == 4 | jbstat == 7 | jbstat == 8 | jbstat == 97
          EDIT: Just to clarify, do you mean that the comment lines at the bottom represent the *values* of jbstat that should correspond to one for each of these dummy variables?
          Last edited by Daniel Schaefer; 27 Jun 2022, 10:18.

          Comment


          • #6
            I am also guessing that when jbstat equals 97, it is missing. Based on your post, I think it is clear you want missing values assigned to "neither". You could of course alternatively preserve missingness on jbstat like so:

            Code:
            gen employed = 0
            replace employed = 1 if jbstat == 1 | jbstat == 2 | jbstat == 5 | jbstat == 6 | jbstat == 9 | jbstat == 10 | jbstat == 11
            replace employed = . if jbstat == 97
            
            gen unemployed = 0
            replace unemployed = 1 if jbstat == 3
            replace unemployed = . if jbstat == 97
            
            gen neither = 0
            replace neither = 1 if jbstat == 4 | jbstat == 7 | jbstat == 8
            replace neither = . if jbstat == 97
            Naturally, the best solution depends on a clear understanding of why the value is missing. If this were survey data and missingness indicated that the person had not responded, I might prefer to impute the missing values rather than assigning them to 'neither'. This does, of course, depend on your particular research question.

            Regardless, you might also want to use -checkvar- to confirm that the new variables match your expectations.

            Code:
            checkvar jbstat employed unemployed neither

            Comment


            • #7
              Hi Daniel,

              The first one was just what I needed - thank you very much!

              I have already dropped the missing, inapplicable, proxy, refused and 'don't know' observations, the 97 variable represented 'doing something else'.

              Best,
              Gabi

              Comment


              • #8
                Note that a little typing can be saved using the inlist() function.
                Code:
                replace employed = 1 if jbstat == 1 | jbstat == 2 | jbstat == 5 | jbstat == 6 | jbstat == 9 | jbstat == 10 | jbstat == 11
                can be shortened to
                Code:
                replace employed = 1 if inlist(jbstat, 1, 2, 5, 6, 9, 10, 11)
                and since inlist() returns a 0/1 value
                Code:
                gen employed = 0
                replace employed = 1 if jbstat == 1 | jbstat == 2 | jbstat == 5 | jbstat == 6 | jbstat == 9 | jbstat == 10 | jbstat == 11
                can be shortened to
                Code:
                generate employed = inlist(jbstat, 1, 2, 5, 6, 9, 10, 11)
                Last edited by William Lisowski; 27 Jun 2022, 13:28.

                Comment


                • #9
                  Awesome, thanks!

                  Comment

                  Working...
                  X