Announcement

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

  • Specificity of "i" in local macros?

    Hi all,

    I have a loop that works, but I would like to understand why it works. Would appreciate your help!

    The data I am working with is made up. It is a combination of birth data (birth ID and the birth month) and monthly precipitation data for January through May (attached as Excel file; could not figure out how to attach as .dta).

    The code creates a new field called "birthprecip" and populates it with the precipitation of the month in which the birth took place. I'm pasting the code below:

    gen birthprecip = .
    local i = 1
    foreach month in jan feb mar april {
    replace birthprecip = precip`month' if birthmo==`i'
    local i = `i'+1
    }

    My specific questions, and what I could figure out for each of them based on playing with the code, are as follows:

    1) Do both of the macros have to use i? Yes, but why?
    I thought local could be used with anything, but "i" appears to have some special meaning, like telling Stata to pay attention to the order.

    2) Why do you need both macros? What is the purpose of each?
    I think the first macro is telling Stata that the value of the first entry in the list is 1. And the second is telling Stata: for each new item in the month list, add 1 to the first i, which was =1. It appears that using "i" also tells Stata not to add 1 to the first observation.

    3) Does the order of the months in the list matter?
    Yes. This is probably related to the special meaning of "i", for which I could not find documentation online or in Stata itself.

    4) Regarding the different things I did with the code (please see attached Do file), why do birthprecip3, 5, 10 and 11 all populate with April data?
    Can't figure this out.

    Thanks,
    Felipe
    Attached Files

  • #2
    There is nothing special about "iI". You could use any other name and get the same results. The reason `i' corresponds to the order in the list of months is that each time you go through the loop you increase it's value by 1. So it starts out at 1 (because of the -local I = 1- command). Then on each successive iteration of the loop, it goes up by 1, hence, 2, 3, 4, thereafter.

    You do need the separate local macro month to provide the spell out of the months as jan feb mar and april. (There are other ways to do it, but all of them involve some second device that contains the text strings jan feb mar and april in that order.) Each time through the loop, local macro month, which is controlled by the -foreach- command advances through the list by one item. Since the -local i = `i' + 1- command also increases `i' by 1 each time through the loop, this establishes a parallel progression of the two macros: 1 with jan, 2 with feb, 3 with mar, 4 with april.

    Consequently, yes, the order of the months in the list is absolutely crucial. That is the only way that Stata will associate jan with month 1, feb with month 2, etc. If you change the order, you will get incorrect results.

    Thank you for trying to show data and code. But please read the Forum FAQ for advice on the most effective ways to do that. First, you should show Stata data, not a spreadsheet. This is done, not by attaching a .dta file, but with the -dataex- command. If you are running version 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.

    As for showing code, the best way to do that is typing or pasting it into this Forum editor window between code delimiters. If you are not familiar with code delimiters, read Forum FAQ #12 for instructions.

    Some people who frequently respond on the Forum are reluctant to download any kind of attachment from strangers. I did not look at you attachments, and I suspect few others did either. So I cannot answer your question 4. If you repost the code and data in the ways I have recommended above, I will give it a look.
    Last edited by Clyde Schechter; 10 Mar 2020, 15:09.

    Comment

    Working...
    X