Announcement

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

  • #16
    A problem with #15 is that second time around the loop the variable already exists and you can't generate it again.

    Another problem is the misuse of inrange(). Also, to test for equality you need == not =.

    I don't know why +1 appears any way -- or why you have 8 high values and 9 low values.

    This should get you closer:

    Code:
    gen gr = .  
    local HI 9 19 29 39 49 59 69 79 .  
    local this = 1  
    forval lo = 0(10)80 {    
         gettoken hi HI : HI    
         replace gr = `this' if  inrange ( age, `lo', `hi')    
         local ++this  
    }
    But all you want here is 10-year bins.


    Code:
    gen gr = 10 * floor(age/10)
    gets you there in one. Also, your bins are then self-describing, as lower limits 0, 10, 20, ..., ...
    Last edited by Nick Cox; 16 Apr 2020, 04:30.

    Comment


    • #17
      Dear Nick,

      Thank you! I am confused if I want 10-year bins for a period between 1800~1890. It looks hard to use your second method.

      Comment


      • #18
        Why be confused? If you want 10 year bins for 1800-1890 that is hard for any method whatsoever as that is 91 years. Tell us what bins you want, and code will follow.

        Comment


        • #19
          Hi, Nick,

          Sorry for my mistake. I just want the 10 years bin for 1800-1889. And, do I really need, "local ++this" for i = i+1? Thank you!

          Comment


          • #20

            Code:
            gen bin = 10 * floor(year/10)
            gives 10-year bins without a loop.

            If you are in a loop then

            Code:
            local i = `i' + 1 
            
            local ++i
            are alternatives.

            Comment


            • #21
              Dear Prof. Nick,

              Thank you a lot!!
              Last edited by Bright Tree; 16 Apr 2020, 10:19.

              Comment


              • #22
                Hi, I am a beginner with stata, please I need help, is it possible to run my categorical variables (outcomes) in groups. example
                I have an outcome that has 3 groups. a b c generalized as A.
                A
                a (
                b (
                c (

                Comment


                • #23
                  Duplicate post. Asked and answered (sort of) at https://www.statalist.org/forums/for...rical-variable.

                  Comment


                  • #24
                    Hello my name is deon

                    I have my age 15, 16, 17, 18. But I will also like to show age range from 40 to 60. So that I can eventually place in a graph. But instead of having all numbers on my graph age 40 age 41 and so on. I am trying to see if it is possible to collapse the data to age range 40 – 60.
                    Thank you

                    HOW OLD |
                    WHEN YOU |
                    FIRST |
                    STARTED | SEX OF RESPONDENT
                    SMOKING? | 1 Male 2 Female | Total
                    -----------+----------------------+----------
                    Age 15 | 664 662 | 1,326
                    Age 16 | 789 974 | 1,763
                    Age 17 | 525 612 | 1,137
                    Age 18 | 952 1,038 | 1,990
                    -----------+----------------------+----------
                    Total | 2,930 3,286 | 6,216

                    Comment


                    • #25
                      I am trying to see if it is possible to collapse the data to age range 40 – 60.
                      It is, indeed, possible. But the details of the code depend on the details of your data. For help with code it is nearly always necessary to show example data in a format that is easily usable by those who would help you. The best way to do that is with the -dataex- command. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

                      So please post back with example data to get specific advice.

                      Comment

                      Working...
                      X