Announcement

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

  • Trying to create a new variable out of the response of the respondents in the survey ! Your help will be much appreciated

    Hello Intelligent People,

    The version of STATA i am using is 14.2 on a Windows 10

    Here is a glimpse of what my data looks like
    methods1 method2 method3 method4 method5
    yes no yes yes yes
    no no no yes no
    no yes yes yes yes
    no no no no no
    yes no no yes yes
    no no no no no
    no no no no no
    yes yes no yes no
    no no no no no
    yes yes no no yes
    So basically i am trying to create a new variable i.e. no_method. I believe that this variable can be derived from the responses of the respondents. So if non of the respondents
    have used any method, it will be counted as an observation of "no_method".
    So from the data above, there are 4 respondents who have used no method at all

    I would be most obliged to you if you could please let me know how to create a new variable no_method.

    Kind Regards

  • #2
    Politeness is always welcome but you would be fine cutting out the fluff
    Your help will be much appreciated

    Today, 14:57
    Hello Intelligent People,

    I would be most obliged to you

    Kind Regards
    and instead using dataex as requested. I (we!) can't tell from your example whether you have string variables or numeric variables with value labels -- which makes precise code difficult to guess.

    Please read https://www.statalist.org/forums/help#stata and while visiting swing by https://www.statalist.org/forums/help#spelling

    The politest postings ask good questions (you have one, hidden) in the way we ask. No need to beg for help!

    Comment


    • #3
      Thanks for your response Nick and apologies for being unclear.

      The variables are Numeric and the Labels are as following:
      1=Yes
      2=No
      8=DK / No opinion
      9= missing


      Comment


      • #4
        Fine, but please present an example with dataex.

        Comment


        • #5
          Sir i have installed the package for dataex. However i am not sure how to generate example data sets there.
          I understand that this makes it easy for the users to help. And i also see that you have been credited for the development for dataex
          Sir could please point me in the right direction

          Comment


          • #6
            Hi Ali,
            I wonder if you have tried something like the following:
            Code:
            * 1 no method
            * 0 some method
            gen nomethod=1
            replace nomethod=0 if methods1==1
            replace nomethod=0 if methods2==1
            So simply repeat that for ALL method variables.
            This assumes that no opinion and missing are equivalent to NO
            HTH
            Fernando

            Comment


            • #7
              #4 You really don't need to call me "Sir". Happens when I am in hotels (fine) or when people think I am an old(er) man (not fine).

              Code:
              help dataex
              explains.

              Otherwise, my guess is that

              Code:
              egen any_2 = anycount(method?), values(2)
              
              gen no_method = any_2 == 5
              may help. (I don't think you need any kind of loop or repetition.)

              First you count how often the methods variables are 2. Then if it's 5 times, all the variables were 2.

              But, but, but:

              1. I have no idea what you want to do about values of 8 or 9.

              2. Your names in 1 are methods1 method2 method3 method4 method5 but my code above assumes that you really meant method1 method2 method3 method4 method5. It is this kind of inconsistency that makes us want examples using dataex.





              Comment


              • #8
                Hello Nick,

                I hope i have done it correctly

                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input double(method1 method4 method2 method3 method5 method6 method7 method8 method9)
                2 1 2 1 2 2 2 2 2
                2 1 2 1 2 2 2 2 2
                1 1 1 1 2 1 2 1 1
                1 1 1 1 2 1 2 1 1
                1 1 1 1 2 1 2 1 1
                1 1 1 1 2 1 2 1 1
                1 1 1 1 2 1 2 1 1
                1 1 1 1 2 1 2 1 1
                1 1 1 1 2 1 2 1 1
                2 1 1 1 2 1 2 1 1
                2 1 1 1 2 1 2 1 1
                2 1 1 1 2 1 2 1 1
                2 1 1 1 2 1 2 1 1
                2 1 1 1 2 1 2 1 1
                2 1 1 1 2 1 2 1 1
                2 1 1 1 1 1 2 1 2
                2 1 1 1 1 1 2 1 2
                2 1 1 1 1 1 2 1 2
                2 1 1 1 1 1 2 1 2
                2 1 1 1 1 1 2 1 2
                2 1 1 1 1 1 2 1 2
                end
                label values method1 CD3A
                label def CD3A 1 "Yes", modify
                label def CD3A 2 "No", modify
                label values method4 CD3B
                label def CD3B 1 "Yes", modify
                label values method2 CD3C
                label def CD3C 1 "Yes", modify
                label def CD3C 2 "No", modify
                label values method3 CD3D
                label def CD3D 1 "Yes", modify
                label values method5 CD3E
                label def CD3E 1 "Yes", modify
                label def CD3E 2 "No", modify
                label values method6 CD3F
                label def CD3F 1 "Yes", modify
                label def CD3F 2 "No", modify
                label values method7 CD3G
                label def CD3G 2 "No", modify
                label values method8 CD3H
                label def CD3H 1 "Yes", modify
                label def CD3H 2 "No", modify
                label values method9 CD3I
                label def CD3I 1 "Yes", modify
                label def CD3I 2 "No", modify

                Comment


                • #9
                  That's fine. The code in #7 applies with a change from 5 to 9.

                  Comment


                  • #10
                    Thanks Nick

                    It worked.

                    Comment

                    Working...
                    X