Announcement

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

  • Skipping and Restarting A Loop

    Hi,

    I have the following data. I am trying to compute the mean for each occupation category in the "occupation_category" variable. I want loop to skip if occupation_category == "Others" and if occupation_category == "Farm Sector".


    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str21 occupation_category long total_income
    "Blue Collar" 15500
    "White Collar" 32000
    "Others" 8000
    "Selfemployed_informal" 36000
    "Farm Sector" 15500
    "Selfemployed_formal" 15500
    "Retired_Aged" 6000
    "Blue Collar" 28000
    "White Collar" 20000
    "Others" 23000
    "Selfemployed_informal" 12000
    "Farm Sector" 6000
    "Selfemployed_formal" 33000
    "Retired_Aged" 9250
    "Blue Collar" 30000
    "White Collar" 15000
    "Others" 9500
    "Selfemployed_informal" 10250
    "Farm Sector" 35000
    "Selfemployed_formal" 14000
    "Retired_Aged" 30000
    "Blue Collar" 6050
    "White Collar" 11000
    "Others" 25000
    "Selfemployed_informal" 35000
    "Farm Sector" 16500
    "Selfemployed_formal" 30000
    "Retired_Aged" 27000
    "Blue Collar" 10000
    "White Collar" 15000
    "Others" 32000
    "Selfemployed_informal" 10000
    "Farm Sector" 32000
    "Selfemployed_formal" 9750
    "Retired_Aged" 32250
    "Blue Collar" 25000
    "White Collar" 32000
    "Others" 17250
    "Selfemployed_informal" 12250
    "Farm Sector" 35000
    "Selfemployed_formal" 6250
    "Retired_Aged" 13000
    "Blue Collar" 17500
    "White Collar" 14000
    "Others" 43000
    "Selfemployed_informal" 10000
    "Farm Sector" 15500
    "Selfemployed_formal" 13250
    "Retired_Aged" 9250
    "Blue Collar" 10000
    "White Collar" 20000
    "Others" 27750
    "Selfemployed_informal" 16500
    "Farm Sector" 35000
    "Selfemployed_formal" 9000
    "Retired_Aged" 30000
    end
    [/CODE]

    I ran the following code. However, it is running acctoss all occupation categories.

    Code:
    levelsof occupation_category, local(occupation)
    foreach i of local occupation {
    
    di "Occupation Category = `i'"
    
    if occupation_category == "Others" & if occupation_category == "Farm Sector"{
    
    }
      tabstat total_income if occupation_category == "`i'" , stat(mean semean) format(%9.0g)
    
    }

    Please help.


  • #2
    use by option,
    Code:
    tabstat total_income if !inlist(occupation,"Farm Sector","Others"), by(occupation) stat(mean semean) format(%9.0g)

    Comment


    • #3

      Thank you, Oyvind Snilsberg for the help.

      I am dealing with a household survey dataset across states and years.
      So, to run the command across states, I use:

      Code:
      tabstat total_income, stat(mean semean) by(state)
      In the sample data, I have provided, I did not add the state information. So, since by() already takes state as its argument, it can't take occupation as another argument in the above command.

      So, I would appreciate it if you can kindly help me with the command in #1.

      Thank you.

      Comment


      • #4
        if you want to use the code in #1,
        Code:
        levelsof occupation, local(occupation)
        foreach i of local occupation {
        if "`i'" != "Others" & "`i'" != "Farm Sector" {
        di "Occupation Category = `i'"
        tabstat total_income if occupation == "`i'",by(state) stat(mean semean) format(%9.0g)
        }
        }

        Comment


        • #5
          Thank you, Oyvind Snilsberg, for the help. That code worked fine.

          Comment

          Working...
          X