Announcement

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

  • Help please, new to stata

    Pleaae help I want to convert a non-numeric character to byte variable.

    . summarize Respondentgender

    Variable | Obs Mean Std. dev. Min Max
    -------------+---------------------------------------------------------
    Respondent~r | 0

    list Respondentgender in 1/10

    +----------+
    | Respon~r |
    |----------|
    1. | Female |
    2. | Male |
    3. | Male |
    4. | Female |
    5. | Male |
    |----------|
    6. | Female |
    7. | Female |
    8. | Male |
    9. | Male |
    10. | Female |
    +----------+



    Respondent |
    gender | Freq. Percent Cum.
    ------------+-----------------------------------
    Female | 11,292 50.03 50.03
    Male | 11,277 49.97 100.00
    ------------+-----------------------------------
    Total | 22,569 100.00

    I have tried these codes with no success :
    encode Respondentgender, gen(female)
    replace female =1 if female == "Female"

    error result: type mismatch
    r(109);

    Thank you in advance

  • #2
    Code:
    encode Respondentgender, gen(female)
    should already have produced a variable female with values 1 and 2 and value labels "Female" and "Male" respectively. As said, its values are 1 and 2, which is why your replace failed. But the replace isn't needed.

    That said, a binary variable with values 1 and 2 is less useful than one with values 1 and 0. So I recommend you now do

    Code:
    replace female = 0 if female == 2 
    label def female 0 "Male", modify
    For more on indicator variables see https://journals.sagepub.com/doi/pdf...36867X19830921

    If you can please edit the thread title to something more like

    Problem in converting string variable to numeric

    Comment


    • #3
      Thank you. I will try it and edit the title accordingly.

      Comment


      • #4
        Sorry, the result I got is not what I wanted.

        desc female

        Variable Storage Display Value
        name type format label Variable label
        ---------------------------------------------------------------------------------------------------------------------
        female long %8.0g female Respondent gender



        label list female
        female:
        0 Male
        1 Female
        2 Male

        I want female as byte, please. Also I want to label define 1 ==female 0 == male

        Comment


        • #5
          One way is to define labels before applying encode, e.g.,

          Code:
          webuse hbp2, clear
          lab def fem  1 "female"  0 "male"
          encode sex, gen(female) lab(fem)
          tab female, nolab
          lab list fem
          Res.:

          Code:
          . tab female, nolab
          
               female |      Freq.     Percent        Cum.
          ------------+-----------------------------------
                    0 |        695       61.61       61.61
                    1 |        433       38.39      100.00
          ------------+-----------------------------------
                Total |      1,128      100.00
          
          . 
          . lab list fem
          fem:
                     0 male
                     1 female
          Otherwise, see

          Code:
          help recode

          Comment


          • #6
            The label for 2 won’t bite as you do not have any such values. You could blank out the label by

            Code:
            label def female 2 “”, modify
            You can use compress to get a byte variable out of a long.

            Comment


            • #7
              Thank you Andrew Musau and Nick Cox. I am getting closer to my intended result: changing the string variables to bytes.

              . list Respondentgender Respondentgender zone SectorUrbanRural female rural in 1/10

              +-----------------------------------------------------------------+
              | Respon~r Respon~r zone Sector~l female rural |
              |-----------------------------------------------------------------|
              1. | Female Female South West Urban Female 2 |
              2. | Male Male North West Rural 2 Rural |
              3. | Male Male North Central Rural 2 Rural |
              4. | Female Female North Central Rural Female Rural |
              5. | Male Male South East Urban 2 2 |
              |-----------------------------------------------------------------|
              6. | Female Female South East Rural Female Rural |
              7. | Female Female South South Urban Female 2 |
              8. | Male Male South South Rural 2 Rural |
              9. | Male Male South South Rural 2 Rural |
              10. | Female Female North East Rural Female Rural |
              +-----------------------------------------------------------------+


              Please how do I turn these variables:
              1. female variable: Female to 1 and then 2 to 0?
              2. rural variable: Rural to 1 and then 2 to 0?

              After these, I should be able to use the same approach to treat the zone.

              Thank you for your help!

              Comment


              • #8
                As I stated in #5, recode will do exactly that.

                Code:
                recode female (2=1) (1=0), gen(wanted)
                tab wanted
                Again, see

                Code:
                help recode

                Comment


                • #9
                  Hello. Thank you Andrew Musau for your help.
                  I am back after trying with not the result I wanted. I tried to turn the variable label "Interest earned on deposits is low" to 1.
                  The variable " lowinterest" is already a byte.


                  replace lowinterest = 0 if lowinterest ==.
                  compress
                  recode lowinterest (Interest earned on deposits is low=1) (0=0), gen(lowint)
                  unknown el Interest in rule
                  r(198);



                  +-----------------------------------------------------+
                  | female rural lowinterest |
                  |-----------------------------------------------------|
                  1. | 1 0 0 |
                  2. | 0 1 0 |
                  3. | 0 1 0 |
                  4. | 1 1 0 |
                  5. | 0 0 0 |
                  |-----------------------------------------------------|
                  6. | 1 1 0 |
                  7. | 1 0 0 |
                  8. | 0 1 0 |
                  9. | 0 1 0 |
                  10. | 1 1 0 |
                  |-----------------------------------------------------|
                  11. | 0 0 0 |
                  12. | 0 1 Interest earned on deposits is low |
                  13. | 0 1 0 |
                  +-----------------------------------------------------+

                  Comment


                  • #10
                    You can only recode a numerical variable, e.g., after encoding a string variable.With a string variable:

                    Code:
                    gen byte lowint= lowinterest=="Interest earned on deposits is low"
                    If it is a numerical variable with value labels:

                    Code:
                    assert !missing(lowinterest)
                    gen byte lowint= cond(!lowinterest, 0, 1)

                    Comment


                    • #11
                      Adebola Daramola : We could have assisted you much easier and effectively if you would have shown us parts of your data using dataex and your code in code delimiters, please read the "Advice on posting to Statalist" in the FAQ, especially sections 12.2 and 12.3.

                      Comment


                      • #12
                        Thank you Andrew Musau and everyone for your guidance and help. I appreciate your help. I will read the FAQs to get the best of Statalist. I look forward to paying it forward with my contribution to the forum soon.

                        Comment

                        Working...
                        X