Announcement

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

  • How to refer to the first element in a macro?

    Hello all

    I am trying to display the content of stored macros in comma separated format , and wanted to put a header before displaying the macros, I am looping across different datasets and different measures. If I used the disp code directly it would be repeated by the number of looped datasets. therefore, I thought of only displaying the header if it was the first element (dataset) in the datasets macro. is there a way to refer to the first element in the macro, without specifying its name i.e if the dataset macro contains "tr12 sk10" , I don't want to refer to tr12 specifically but rather the first element so that the code can be more general? or is there some other way to disp a header in a loop to be displayed only once.

    Thanks in advance
    R

  • #2
    Here are three ways to do it:

    Code:
    . local foo tr12 sk10
    
    . tokenize "`foo'"
    
    . di "`1'"
    tr12
    
    . di word("`foo'", 1)
    tr12
    
    . di "`: word 1 of `foo''"
    tr12

    Comment


    • #3
      Thank you Nick for your reply. however, when I apply the following code (part from the loop) i get the following error message,

      tokenize " `dataset' "
      if ` dataset' ="1" di "dataset,measure1,measure2 "
      di "` dataset',`,m1',`m2' "
      tr12 not found

      I also got the same message when I was trying to use (if i=1/"`ccyy' " di "dataset,measure1,measure2 " )

      I think there is something wrong with the code that I cannot figure it out.

      Thanks in advance!

      Comment


      • #4
        You're right. There are errors in your code: I spotted five.

        1. Should be if "`dataset'" not if `dataset'

        2. (Corrected above) Remove spaces from macro references. "` dataset'" should be "`dataset'"

        3. Use == rather than = for testing equality.

        4. A literal test for equality with "1" doesn't seem what you want here.

        5. Your last statement seems to need an else


        There may be others, but I can't check given that you haven't shown us the definitions of all the macros.
        Last edited by Nick Cox; 01 Feb 2018, 04:58.

        Comment

        Working...
        X