Announcement

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

  • Executing a repetitive command satisfying conditions

    Good morning everyone,
    I have a dataset that looks like this:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str15 new_comm double price
    "Apples"                        140
    "Apples"                        232
    "Apples"                        235
    "Apples"                        238
    "Apples"                        230
    "Apples"                        280
    "Apples"                        230
    "Apples"                        246
    "Apples"                        246
    "Apples"                        290
    "Apples"                        290
    "Apples"                        325
    "Apples"                          .
    "Apples"                          .
    "Apples"                          .
    "Apples"                        500
    "Apples"                        500
    "Apples"                        500
    "Apples"                        400
    "Bananas"                       180
    "Bananas"                       193
    "Bananas"                       187
    "Bananas"                       174
    "Bananas"                       170
    "Bananas"                       220
    "Bananas"                       200
    "Bananas"                       192
    "Bananas"                       176
    "Bananas"                       200
    "Bananas"                       250
    "Bananas"                       250
    "Bananas"                         .
    "Bananas"                         .
    "Bananas"                         .
    "Bananas"                       240
    "Bananas"                       230
    "Bananas"                       270
    "Bananas"                       240
    "Beans"                         260
    "Beans"                         204
    "Beans"                         187
    "Beans"                         190
    "Beans"                         160
    "Beans"                         180
    "Beans"                         180
    "Beans"                         160
    "Beans"                         160
    "Beans"                         150
    "Beans"                         190
    "Beans"                         160
    "Beans"                           .
    "Beans"                           .
    "Beans"                           .
    "Beans"                         170
    "Beans"                         205
    "Beans"                         190
    "Beans"                         180
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                           .
    "Bread"                           .
    "Bread"                           .
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Bread"                          10
    "Oil (vegetable)"               120
    "Oil (vegetable)" 115.4000015258789
    "Oil (vegetable)" 115.4000015258789
    "Oil (vegetable)" 115.4000015258789
    "Oil (vegetable)"               116
    "Oil (vegetable)"               112
    "Oil (vegetable)"               112
    "Oil (vegetable)"               114
    "Oil (vegetable)"               114
    "Oil (vegetable)"               114
    "Oil (vegetable)"               115
    "Oil (vegetable)"               120
    "Oil (vegetable)"                 .
    "Oil (vegetable)"                 .
    "Oil (vegetable)"                 .
    "Oil (vegetable)"               116
    "Oil (vegetable)"               112
    "Oil (vegetable)"               116
    "Oil (vegetable)"               116
    "Onions"                         88
    "Onions"                        126
    "Onions"                        149
    "Onions"                         48
    "Onions"                         40
    end
    Now, my aim is to run a command like
    Code:
    mdesc price if new_comm=="Rice"
    for all the different categories of commodities. Since I have 20 commodities I thought about using a loop. I tried with
    Code:
    foreach x in new_comm {
              mdesc price
              }
    but it doesn't do what I want.

  • #2
    Quite. It's a common error to suppose that a loop in varname is a loop over the distinct values of a variable varname. It isn't.

    Your loop is legal but will just execute mdesc once.

    mdesc is from SSC, as you are asked to explain. As is prominent in the help it supports by: so

    Code:
    bysort new_comm : mdesc price
    should do what you want.

    Code:
    sysuse auto, clear
     bysort foreign : missings report rep78 , percent
    is an example of an equivalent command, and missings -- from the Stata Journal -- is more versatile than mdesc.
    Last edited by Nick Cox; 07 Oct 2023, 07:23.

    Comment


    • #3
      Nick Cox thank you for the clarification!

      Comment

      Working...
      X