Announcement

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

  • Why the unab result would not be displayed?

    Click image for larger version

Name:	Screen Shot 2023-04-10 at 10.44.37.png
Views:	2
Size:	71.0 KB
ID:	1709118
    Hi all statalisters,

    I'm confused by the unab function when the result cannot be displayed. I guess it is because the unab function doesn't work as command. Here's the screenshot

    After I input display, it doesn't work. I wonder if the unab function doesn't work out or if I need to reset something.

    Thank you !!
    Attached Files

  • #2
    You see that "end of do file" after the -unab- command? That's the reason the display command shows nothing. -unab- creates a local macro, and local macros go out of scope (disappear) at the end of the program or do-file that creates them. This is a common error people make when using Stata, trying to run code that contains local macros one line at a time. That never works. When local macros are involved, the entire code from the creation of the local macro through the last reference to it must be run all together without interruption.

    Here's a short do-file:
    Code:
    clear*
    
    sysuse auto
    
    unab X: price mpg weight
    
    display "`X'"
    When I run it all at once from beginning to end you get the result of -unab- displayed:
    Code:
    . do "C:\Users\clyde\AppData\Local\Temp\STD6158_000000.tmp"
    
    . clear*
    
    .
    . sysuse auto
    (1978 automobile data)
    
    .
    . unab X: price mpg weight
    
    .
    . display "`X'"
    price mpg weight
    
    .
    end of do-file
    But if I run it line by line:
    Code:
    . do "C:\Users\clyde\AppData\Local\Temp\STD6158_000000.tmp"
    
    . clear*
    
    .
    end of do-file
    
    . do "C:\Users\clyde\AppData\Local\Temp\STD6158_000000.tmp"
    
    . sysuse auto
    (1978 automobile data)
    
    .
    end of do-file
    
    . do "C:\Users\clyde\AppData\Local\Temp\STD6158_000000.tmp"
    
    . unab X: price mpg weight
    
    .
    end of do-file
    
    . do "C:\Users\clyde\AppData\Local\Temp\STD6158_000000.tmp"
    
    . display "`X'"
    
    
    .
    end of do-file
    local macro X is destroyed immediately after it is created, and the subsequent attempt to display it produces no output.



    Comment


    • #3
      It worked so perfectly and make all sense to me! @Clyde Schechter Thank you so much for immediate response!!
      Sorry I have an additional question: the local macro runs at once and cannot be runned line by line? What about the global macro?
      Thank you!

      Comment


      • #4
        Global macros are visible everywhere once they have been defined. That's not a good reason to use them. In any case unab doesn't produce a global macro as result, even optionally. You would need to copy what it creates to a global macro, and that's not recommended.

        More on this problem -- which is one of understanding, not one of use -- at https://journals.sagepub.com/doi/pdf...36867X20931028

        Note that unab is a command, not a function. In Stata, commands and functions are disjoint, so that function is not another name for command.

        Comment


        • #5
          @Nick Cox make sense! Now I understand the difference and how macro works, thank you !

          Comment

          Working...
          X