Announcement

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

  • Running forvalues for two variable values

    I am trying to calculate the duration for which students were in public, private, and homeschool during their school career while being exposed to 40 types of infection.
    q37_1 and q38_1 define the start and end age of schooling reported by students for the first type of infection. I am unable to run the forvalues command when I type "forval v= a/b"
    Can someone suggest a solution?

    Code:
    *********start age of infection************************************
    forval k=1/40{
        
    recode q37_`k' (14=.)
    replace q38_`k'=. if q37_`k'==. //can't calculate duration of infection if intial point is missing
    replace q38_`k'=. if q38_`k'>13 // excluding datapoints for beyond school age
    
    * creating schooling variables for each type of infection
    gen pubA`k'=0
    gen privA`k'=0
    gen homeA`k'=0
    
    gen a=q37_`k'
    gen b=q38_`k'
    
    //calculating total years by schooling type for each infection
    
    forval v= a/b {
    replace pubA`k'=pubA`k' + 1 if q7_`v'==1 | q7_`v'==5
    replace privA`k'=privA`k' + 1 if q7_`v'==2 | q7_`v'==3 | q7_`v'==4
    replace homeA`k'=homeA`k' + 1 if q7_`v'==6
    }
    
    drop a b
    }

  • #2
    try locals instead of variables,
    Code:
    local a = q37_`k'
    local b = q38_`k'
    
    forval v = `a' / `b' {
    ...
    }

    Comment


    • #3
      I had tried putting local. STATA gives invalid syntax
      r(198);

      Comment


      • #4
        right, I suppose you need to skip the second loop if a or b is missing. perhaps if you could post some example data using -dataex-

        Comment


        • #5
          Thank you for raising the issue of missing data. I put the following code and it worked
          Code:
          if !missing(q37_`k'){
          local a=q37_`k'
          local b=q38_`k'
          
          forval v= `a'/`b' {
          replace pubA`k'=pubA`k' + 1 if q7_`v'==1 | q7_`v'==5
          replace privA`k'=privA`k' + 1 if q7_`v'==2 | q7_`v'==3 | q7_`v'==4
          replace homeA`k'=homeA`k' + 1 if q7_`v'==6
          }
          }

          Comment


          • #6
            I realized that the macros are not storing the variable's cell values, and I got wrong results. Instead I thought of reshaping data to a panel format. I have attached a portion of my data as sample.dta
            cid is the ID
            grade goes from 1 to 13 for K-12 grades
            q7_ is the school type attended at each grade
            q37_1 is the start grade of treatment
            q38_1 is the end grade of treatment

            q37_1 and q38_1 are self reported, and q38_1 should be greater than or equal to q37_1 if a respondent entered correct information

            For each cid, I want to calculate the duration of treatment by each school sector contained in q7_1.

            Thanks for your help
            Attached Files

            Comment


            • #7
              Code:
              bys cid q7_ (grade): gen wanted = q38_1 - q37_1

              Comment


              • #8
                That code does not give me the right information. For example, for cid 537452953 the value of wanted is always 12. The treatment q37_1 started at kindergarten (=1) and ended at grade 12 (=13). I wanted to calculate how much time of that treatment was spent in each school sector of q7_1 (such as 2 years in charters, 3 in private, 2 in homeschool etc).

                Comment


                • #9
                  I see, my bad. Try,
                  Code:
                  bys cid q7_ (grade): gen wanted = _N

                  Comment


                  • #10
                    Unfortunately, that code is again wrong.

                    Comment


                    • #11
                      do you mind filling in the variable wanted,
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input long cid byte(grade q7_ q37_1 q38_1) float(code_no7 code_no9 wanted)
                      537452953  1 1 1 13 12 4 .
                      537452953  6 1 1 13 12 4 .
                      537452953  9 1 1 13 12 4 .
                      537452953 11 1 1 13 12 4 .
                      537452953  2 2 1 13 12 3 .
                      537452953  4 2 1 13 12 3 .
                      537452953  7 2 1 13 12 3 .
                      537452953  8 3 1 13 12 3 .
                      537452953 10 3 1 13 12 3 .
                      537452953 12 3 1 13 12 3 .
                      537452953  3 4 1 13 12 3 .
                      537452953  5 4 1 13 12 3 .
                      537452953 13 4 1 13 12 3 .
                      end
                      label values q7_ labels24
                      label def labels24 1 "Public school", modify
                      label def labels24 2 "Private Christian school", modify
                      label def labels24 3 "Other religious or non-Christian private school", modify
                      label def labels24 4 "Private school not associated with a particular faith", modify
                      label values q37_1 labels138
                      label def labels138 1 "Kindergarten / 5 years old", modify
                      label values q38_1 labels178
                      label def labels178 13 "12th grade / 17 years old", modify
                      ?

                      Comment


                      • #12
                        For that cid 537452953, the treatment started in kindergarten and ended in grade 12, the person was in public school for grades 1, 6, 9, and 11 (4 years), and in private schools for all other grades (9 years).
                        Code:
                         * Example generated by -dataex-.
                        To install: ssc install dataex clear input long cid byte(grade q7_ q37_1 q38_1) float(code_no7 code_no9 wanted)
                        537452953  1 1 1 13 12 4 4
                        537452953  6 1 1 13 12 4 4
                        537452953  9 1 1 13 12 4 4
                        537452953 11 1 1 13 12 4 4
                        537452953  2 2 1 13 12 3 9
                        537452953  4 2 1 13 12 3 9
                        537452953  7 2 1 13 12 3 9
                        537452953  8 3 1 13 12 3 9
                        537452953 10 3 1 13 12 3 9
                        537452953 12 3 1 13 12 3 9
                        537452953  3 4 1 13 12 3 9
                        537452953  5 4 1 13 12 3 9
                        537452953 13 4 1 13 12 3 9
                        end
                        label values q7_ labels24 label def labels24 1 "Public school", modify
                        label def labels24 2 "Private Christian school", modify
                        label def labels24 3 "Other religious or non-Christian private school", modify
                        label def labels24 4 "Private school not associated with a particular faith", modify
                        label values q37_1 labels138
                        label def labels138 1 "Kindergarten / 5 years old", modify
                        label values q38_1 labels178
                        label def labels178 13 "12th grade / 17 years old", modify
                        Last edited by Dany Shakeel; 27 Jan 2022, 04:38.

                        Comment


                        • #13
                          thank you, sorry for not rereading the op,
                          Code:
                          recode q7_ (1 5 = 1 "Public school") (2 3 4 = 2 "Private school") (6 = 3 "Homeschooling"), gen(sector)
                          bys cid sector (grade): gen wanted = _N

                          Comment


                          • #14
                            I appreciate your help, but these codes are not delivering the correct results. For example, for cid 537453053 the treatment q37_1 started in grade 9 and ended in grade 12 (q38_1). In those grades, the individual was always in a private school. The cell values for wanted show 6 years in private school and 7 in public school.

                            Comment


                            • #15
                              final attempt...
                              Code:
                              bys cid (grade): gen x = grade>=q37_1 & grade<=q38_1
                              
                              bys cid (grade): egen treatpub = total(x & inlist(q7_,1,5))
                              bys cid (grade): egen treatpriv = total(x & inlist(q7_,2,3,4))
                              bys cid (grade): egen treathome = total(x & q7_==6)

                              Comment

                              Working...
                              X