Announcement

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

  • working with local string macro and delimiters

    I'm converting a SAS code into STATA so it's more accessible to people at work.

    The original SAS code is:

    %IF (&YEAR GE 1998) %THEN %DO;
    %LET ALSOKEEP=%STR(&ALSOKEEP BCALL BMAGZNEWS BMAILADTV BINTERNET
    BFRIENDWORK BFINPRO BFINPLAN BSELF BDONT BOTHER
    ICALL IMAGZNEWS IMAILADTV IINTERNET IFRIENDWORK
    IFINPRO IFINPLAN ISELF IDONT IOTHER);
    %END;

    and what it's doing is adding more variable names to a macro ALSOKEEP.


    In STATA, I've written this
    #delimit;
    if (`YEAR'>=1998) {;
    local ALSOKEEP="`ALSOKEEP'"+" BCALL BMAGZNEWS BMAILADTV BINTERNET
    BFRIENDWORK BFINPRO BFINPLAN BSELF BDONT BOTHER
    ICALL IMAGZNEWS IMAILADTV IINTERNET IFRIENDWORK
    IFINPRO IFINPLAN ISELF IDONT IOTHER";
    };
    #delimit cr

    And the problem is, because the list of additional variables is in a quote, the formatting (line change and indentation) is also saved in the macro, when what I want is just a long line of code that the computer takes in. For instance, this is what is saved into ALSOKEEP.

    Y1 YY1 SAVED BCALL BMAGZNEWS BMAILADTV BINTERNET BFRIENDWORK BFINPRO BFINPLAN BSEL
    > F BDONT BOTHER ICALL IMAGZNEWS IMAILADTV IINTERNET IFRIENDWORK
    > IFINPRO IFINPLAN ISELF IDONT IOTHER

    where Y1 YY1 SAVED is the original value of ALSOKEEP that the long string was added to. You see, there is indentation between each line instead of one space. The best alternative I've thought of that would keep the code readable and formatted right is this:

    #delimit;
    if (`YEAR'>=1998) {;
    local ALSOKEEP="`ALSOKEEP'"+" BCALL BMAGZNEWS BMAILADTV BINTERNET"
    +" BFRIENDWORK BFINPRO BFINPLAN BSELF BDONT BOTHER"
    +" ICALL IMAGZNEWS IMAILADTV IINTERNET IFRIENDWORK"
    +" IFINPRO IFINPLAN ISELF IDONT IOTHER";
    };
    #delimit cr

    but it looks rather messy and requires more work as I have many of these (much longer) to repeat.

    Also, if possible, I'd appreciate a solution where I don't need to use delimiters. Universal delimiter use in my code was causing problem with declaring local macros, so now I'm spot-using it.

    Thanks in advance.

  • #2
    Not your question, but watch out. I've never used SAS but my understanding is that its condition

    Code:
    %IF (&YEAR GE 1998)
    implies a loop over observations. On the other hand, in Stata

    Code:
    if year > 1998 {
    
    }
    implies no loop, only a branch that is a branch on the first observation only:

    Code:
    if year[1] > 1998 {
    
    }
    which, for your kind of problem, is most unlikely to be what you want. For more, see http://www.stata.com/support/faqs/pr...-if-qualifier/

    On your question, you needn't use quotation marks at all to play with lists of variable names. Run this for all the technique you need for that purpose.

    Code:
    sysuse auto, clear  
    
    local mynames mpg
    
    local mynames `mynames'  weight price
    
    mac li
    The key point is that variable names cannot themselves include spaces or quotation marks, so concatenation is enough. No double quotation marks are needed.

    Comment


    • #3
      Thank you! `YEAR' is one of the inputs of this program and not a vector/variable, but thanks for bringing up your concern.

      Comment

      Working...
      X