Announcement

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

  • Stata local macros not displaying

    Hello,
    I am trying to rescale weights within a single NSFG survey wave to have a mean of 1, however Stata is not returning anything when I use the 'display' command. Stata also says invalid syntax when I attempt to multiply the weight by the new scaling factor, but I can't find anything wrong with it. Is there something small here that is wrong, or am I misunderstanding something about how local macros and scalars work?

    summarize finalwgt
    return list
    local rescalefactor = `r(N)'/`r(sum)'
    display `rescalefactor' /// This is the line that returns nothing to me
    gen weight2 = .
    replace weight2 = finalwgt*`rescalefactor' // Stata error says invalid syntax
    summarize weight2

    Thank you in advance!

  • #2
    Well, I loaded in a data set that contains a variable named finalwgt and ran your code. I cannot reproduce your problem. Instead, I find a different problem. Instead of returning nothing, that line throws an error message:
    Code:
    . display `rescalefactor' /// This is the line that returns nothing to me
    > gen weight2 = .
    .00008835gen not found
    This is happening because of the ///. /// declares the material that follows on that line to be comment, but it also tells Stata that the command continues on the next line. Of course, -genweight2 = .- is a separate command and is not a valid continuation of -display `rescalefactor'.

    When I change that /// to // it runs just fine. // declares the material that follows on that line to be comment, but also tells Stata that the command is complete.

    Comment


    • #3
      Ah, I apologize- I didn't run the code with those comments- I only added them into the blog post to indicate where I was having trouble.

      Comment


      • #4
        Well, there's nothing wrong with the code. The other likely explanation is that you are trying to run this code one line a time, or in blocks of some kind. You can't do that with code involving local macros. When the line or group of lines you run ends, any local macros defined in that block of code disappear. So if you try to refer to them later, they are read as empty strings. If you run the code in one fell swoop from beginning to end, you will overcome that.

        Comment


        • #5
          Yep, that was the important detail I was missing! I had always heard that locals exist until I exit Stata (exist "within" the program or do-file)- I didn't realize that they only existed within a command or single code block. Thanks so much Clyde!

          Comment


          • #6
            I didn't realize that they only existed within a command or single code block
            Any selection of lines is not a code block in the Stata ado language. A local macro defined in a code block is available outside the code block:
            Code:
            if (1==1) { // start first block 
                
                local one = 1
            } 
            
            if (2==2) { // start second block
                
                local two = 2
            } 
            
            macro dir _one _two
            Code:
            _one:           1
            _two:           2

            Comment


            • #7
              Bjarte Aagnes is right, I should have chosen my words more carefully. What I meant to say is that if you select a group of lines in the do-file editor and run them, any local macros defined therein go out of existence immediately aftereward. But a block has a different meaning from selected group of lines.

              Comment

              Working...
              X