Announcement

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

  • Seqentially Adding Elements to Global

    I am trying to use for loops to build up the code for the collapse command. I want to end up with globals with elements of the form "p90age = age"
    Code:
    global details "age occ ind wkswork1 inc  poverty occscore movedin "
    
    
    global ptiles "1 5 10 25 50 75 90 95 99"
    
    foreach p of global ptiles {
    global pc`p' ""
        foreach v of global details {
            global pc`p' " $pc`p' P_`v'`p'= `v' "
            }
    }
    My hope was that I would end up with 9 globals each containing the necessary code for a collapse command of the form:

    Code:
    collapse p(10) $pc10 p(25) $pc25 , by(year)
    But, instead each global ends up with only the last element in it. E.g. pc99 =" P_movedin99 = moved". What is strange is that I can add elements manually with essentially the same syntax. I'd be very grateful for any advice.

    Thanks,

    Stu

  • #2
    Perhaps this will start you in a useful direction. The key is that sometimes braces {} need to be used to indicate the beginning and end of the global variable name that follows the dollar sign.
    Code:
    . global details "age occ"
    
    . global ptiles "10 25"
    
    . foreach p of global ptiles {
      2.     global pc`p' p`p'
      3.     foreach v of global details {
      4.         global pc`p' ${pc`p'} P_`v'`p'=`v'
      5.         }
      6.     display " pc`p' : ${pc`p'}"
      7. }
     pc10 : p10 P_age10=age P_occ10=occ
     pc25 : p25 P_age25=age P_occ25=occ
    
    .

    Comment


    • #3
      Brilliant! Thanks so much. I always thought the braces were a matter of taste but now I know otherwise.

      Comment


      • #4
        I will just add that one advantage of using a local macro instead of a global macro is that you never encounter this problem of needing braces to clarify where the macro name ends. The ` starts a local macro and the ' at the same level of embedding ends it. There's never any ambiguity about it.

        And also remember that the use of global macros is an unsafe programming practice which should be avoided except when there is no viable alternative. Since local macros are as good (actually better) for lists of the kind involved here, the local macro would be a better choice even if there were no name ambiguity issues.

        Comment


        • #5
          At the risk of sounding ignorant why are globals considered unsafe? I mostly use them for path names etc, but sometimes use them when I am working both in the do file editor and interactively so that I don't have any issues. Or is that precisely why they are unsafe?

          Comment


          • #6
            ... because they are global, and so you can unwittingly overwrite what is the same global defined elsewhere.

            Comment


            • #7
              Things I should have known long ago! Having gone back to re-read the manual I now see that locals are file dependent and globals aren't. Thanks very much for prompting me to look into it properly.

              Comment


              • #8
                Indeed. Your question about safety is the most important, but here's another detail. If you see a global and you didn't define it and you don't know what it is, or where it is defined, how do you find out? In principle it could be defined anywhere. Naturally you can always look inside to get an idea, as its value is accessible everywhere too.

                Comment

                Working...
                X