Announcement

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

  • how to use an asterisk(*) for multiple variables with an "if" condition

    Dear all, i have an issues which iam trying to figure out but i am not getting anywhere and i need ur help. For example i have several individuals in a household with different sex and i need to use for example "count sex* if age*<13 & age*>6 & sex*=="male" " this seems not to work. any advice on how to run this and it picks all the numbers required?
    Last edited by Deogratious Sebuwufu; 26 Sep 2021, 14:53.

  • #2
    That's illegal, which explains why it doesn't work.

    What are the names of your variables? Do you want just

    the number of males who are aged 7-12

    because typically that would depend on an age variable and a sex variable?

    Comment


    • #3
      I have a dataset which has household members and in that dataset, it records all the people with different sex [person1(sex1), person2(sex2), person3(sex3).....person10(sex10)] and different age (groups [person1(age1), person2(age2), person3(age3).....person10(age10)]) as well. that means, if in the household i have ten(10) members, i will have males and females. Secondary, since i am interested in knowning the sex of the age group ( 7 -12yrs) of interest and for a particular sex for example "males", that would be of importance to me. I did it in excel and i get results but i don't see reasons why i kept going to excel. i want to use start fully with syntaxes for anyone who wants to proof read my analysis.
      Last edited by Deogratious Sebuwufu; 27 Sep 2021, 05:35.

      Comment


      • #4
        I think you may need to give a data example. Do look at https://www.statalist.org/forums/help#stata

        Otherwise it sounds as if you may want to use one of the functions in egen whose names start with any or row.

        Alternatively, your data structure may be better reshaped so that each observation is a person not a household.

        Comment


        • #5
          below is an example which i have pulled from my dataset
          uniqueid hhrostersex1 hhrosteryr1 hhrostersex2 hhrosteryr2 hhrostersex3 hhrosteryr3 hhrostersex4 hhrosteryr4 hhrostersex5 hhrosteryr5 hhrostersex6 hhrosteryr6 hhrostersex7 hhrosteryr7 hhrostersex8 hhrosteryr8 hhrostersex9 hhrosteryr9 hhrostersex10 hhrosteryr10
          11036174101
          1103617411 Female 49 Male 54 Male 20 Male 19 Male 17 Male 15 Female 10 Male 8 Male 16
          11036174111 Female 58 Male 63 Male 18 Male 15 Male 4
          11036174121 Female 23 Male 23 Female 43 Female 14 Female 7 Male 15 Male 12 Female 1
          11036174131 Female 30 Male 54 Female 45 Female 14 Female 13 Male 16 Male 21 Female 19 Female 5
          11036174132 Female 23 Male 30 Male 4 Female 2
          11036174141
          11036174151 Male 46 Female 41 Male 20 Male 18 Female 17 Male 16 Female 14 Female 12
          11036174161 Female 38 Male 43 Female 6 Female 3 Female 1
          11036174171 Male 63 Female 38 Male 14 Male 10 Female 3
          11036174181
          11036174191 Female 20 Male 23 Female 12 Male 2
          11036174201 Female 22 Male 22
          1103617421
          11036174211 Female 66 Male 71 Female 15 Male 9
          11036174221 Female 20 Male 30 Female 12
          11036174231 Female 15 Male 43 Female 38 Female 13 Male 11 Female 10 Male 2
          11036174241 Female 35 Male 44 Female 20 Male 13 Female 11 Male 10 Male 7 Female 3
          11036174251 Male 38 Female 29 Male 12 Male 10 Male 10 Female 6 Female 5
          11036174261 Male 48 Female 35 Female 2
          11036174271 Male 27 Female 22 Male 3 Male 2
          11036174281 Male 76 Female 70 Male 5
          11036174291 Female 65

          Comment


          • #6
            Unless you use dataex as requested I have no way of working out whether your sex variables are string or numeric with value labels.

            Comment


            • #7
              the sex variable is coded and its numeric (1"Male" 2"Female")

              Comment


              • #8
                Still not using dataex. A little progress is possible but hard work for the reader. Let's show the principles by addressing the syntax that wouldn't work

                Code:
                count sex* if age*<13 & age*>6 & sex*=="male"
                This syntax is wrong (1) because your variable names don't match (2) because wildcards can't appear in this command (3) because your sex variables are numeric.

                I think this needs to be a loop

                Code:
                gen age7_12male = 0 
                
                forval j = 1/10 { 
                       replace age7_12male = age7_12male + (hhrostersex`j' == 1  & inrange(hhrosteryr`j', 7, 12))  
                }
                You need to look at each pair of variables and check whether you have suitable sex and age to include it in the count.

                Your present data structure obliges this. A reshape long would help for this purpose, but perhaps not other purposes.

                Comment


                • #9
                  Hello Cox, i have a pressing question. How can i run cross tabulations, create dummy variables and run a regression with a dataset in stata in wide format. The solutions you gave me last time were the best and i expanded on that.

                  Comment


                  • #10
                    Please start a new thread. However, you’re asking for code for a entire task, which is optimistic.

                    I am travelling now and restricted to short answers.

                    Comment


                    • #11
                      Thanks. I will be honoured by your replay. I have also posted the thread "how to create a cross-tabulations, dummy variables and regression with wide data"

                      Comment


                      • #12
                        See

                        https://www.statalist.org/forums/for...with-wide-data

                        for the thread mentioned in post #11.

                        Comment


                        • #13
                          can't i just do the analysis without using the reshape command?

                          Comment

                          Working...
                          X