Announcement

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

  • using local as range in for values

    Hi Statalist

    I would like to run a loop for every value within a variable called "spell_count", and then run the whole do.file as a loop for different datasets, where the range for "spell_count" changes.

    In file one the range is 1 - 82. So I can run

    forvalues n = 1(1)82 {

    if spell_count == `n' (etc etc)

    }

    but as the range in spell count changes across datasets, I would like to specify the range as a local, and then use this within the for values command.

    I can define the range using a local, but then get an error when I put this in to forvalues:

    quitetly sum spell_count
    local max = `r(max)'
    local min = `r(min)'

    forvalues n = `min'(1)`max' {

    if spell_count == `n' (etc etc)

    }

    Does anyone know what I am doing wrong?

    Best Wishes

    Joe

  • #2
    Code:
    levelsof spell_count,local(spells)
    foreach n of local spells{
    if spell_count == `n' (etc etc)
    }

    Comment


    • #3
      Perfect - thank you Ali!

      Comment


      • #4
        #2 is not going to work either -- unless I am drastically misunderstanding the data structure.

        The key here is that the if command and the if qualifier are different beasts and not two choices for doing the same thing. English or very likely most other languages is no guide here as although

        If I can answer a question, then I will

        and

        I will answer a question if I can

        just look like stylistic variants on each other, that does not carry over to Stata. Closer to home, there are, I believe, statistical languages in which code like #1 or #2 carries it an implication that the program will loop over observations for you, but that is not what Stata promises or does here.

        https://www.stata.com/support/faqs/p...-if-qualifier/ says more but to me it is written backward. The question that is supposedly asked is in fact the answer and the usual question is that an
        if command is not working as expected.

        Picking up Ali's code,


        Code:
          
         if spell_count == `n'
        will always be interpreted as

        Code:
          
         if spell_count[1] == `n'
        which is I guess only once what you want and otherwise quite wrong. I agree: it's not obvious, or even especially well documented. that Stata does this, although the logic is that the if command can only yield one bit of information, true or false. More obviously, it is the job of the if qualifier to evaluate truth or falsity observation by observation.

        Depending very much on all that you aren't showing us something more like this may get you closer



        Code:
        sum spell_count, meanonly
        
        forvalues n = `r(min)'/`r(max)' {
        
        some_stuff if spell_count == `n'
        
        }





        But there are lots of puzzles here. For one, "dataset" has a meaning to Stata which is whatever could be saved as a .dta file. I don't know what meaning it has in #1.

        More importantly, whether what you want is best done as a loop is not obvious either.

        On a micro level consider this story from say a children's book.

        I have a pencil.

        I put it in a box.

        I take the pencil out of the box.

        I now can use my pencil.

        The reaction should be just skip the boxing and unboxing unless there is a separate good reason. for it. Here for box read macro and see from the code above that you can do directly what you want.

        Comment


        • #5
          Dear Nick -

          Thank you for your response. I apologise - i did not make it clear the description I made within the loop was paraphrasing the commands I wanted to execute in the loop. When I wrote

          if spell_count == `n'

          i should have written

          replace x = y if spell_count == `n'.

          The thing I was struggling with is how to let Stata know the range in "spell_count" which I need for forvalues differs when I import filename_a.dta and then file_name_b.dta.

          Ali's method worked for this purpose.

          Sorry for the confusion

          Best Wishes

          Joe

          Comment


          • #6
            I don't understand #5 as the whole point of #4 is that the if command does not do what you want. so Ali's code doesn't give the solution.

            It seems that you solved your problem, which is excellent, but the thread is dangerous to later readers if they pick up an idea that the
            if command and the if qualifier are somehow equivalent. Just occasionally they give the same result, but not in general.

            Comment

            Working...
            X