Announcement

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

  • Extract last element of local

    How can I extract the last element in a local (without knowing how many elements the local has)

    Code:
    local list one two three four last

  • #2
    Consider the following use of the -word()- function:

    Code:
    local list one two three four last
    di word("`list'", `=wordcount("`list'")')
    Res.:

    Code:
    . local list one two three four last
    
    . di word("`list'", `=wordcount("`list'")')
    last

    Comment


    • #3
      nice thanks

      Comment


      • #4
        Start by figuring out how many elements the local has.
        Code:
        . local list one two three four last
        
        . local n : word count `list'
        
        . local l : word `n' of  `list'
        
        . display "last item is `l'"
        last item is last
        
        .

        Comment


        • #5
          Here's another way to do it which I suggest is simpler:

          Code:
          . di word("a b c", -1)
          c
          The example is just to make the point. In practice, you feed the function your macro, not a literal string where the answer is obvious.

          Comment


          • #6
            Thanks to all, I learned a lot!

            Not 100% related, but when I used this for numbers which I then inserted into "range" in a figure and it gave me the error

            Code:
            range() invalid -- invalid numlist
            even though the local actually stores the correct number. Is that because when I use "word" it stores it as a string and that is different from a number for the graph command?

            Comment


            • #7
              It isn't clear from the information provided, but if you run the plotting command independently of defining the local, then the local is empty. You would inevitably get the error message. See https://journals.sagepub.com/doi/abs...urnalCode=stja

              Comment


              • #8
                Here is an MWE to reproduce the error

                Code:
                sysuse auto.dta, replace
                
                local bins 30 35 40 45 50
                
                local min word("`bins'", 1)
                local max word("`bins'", -1)
                
                disp `min'
                disp `max'
                
                twoway scatter displacement turn, xscale(range(30 50))
                twoway scatter displacement turn, xscale(range(`min' `max'))
                Not implying that this makes any sense here, it's just to get the error

                Comment


                • #9
                  You need (e.g.)

                  Code:
                  local min = word("`bins'", 1)
                  which places the result of the calculation in the macro.

                  Otherwise you just get two chunks of text each starting with word( being fed to range() and Stata is right: that is not a numlist.

                  Comment


                  • #10
                    excellent, thanks!

                    Comment

                    Working...
                    X