Search code examples
listargumentsstatastata-macros

Passing multiple lists of unspecified length to a program


I would like to create a Stata program that takes multiple lists of unspecified length as arguments. However, I don't know how the program can distinguish between the lists once they're passed in.

For example, I'd like to be able to do something like:

prog myprog
args list1 list2
{something with list1}
{something with list2}
end

loc list1 a b c
loc list2 x y z
myprog `list1' `list2'

loc list1 a b c d
myprog `list1' `list2'

The two solutions I've been thinking about are:

  1. Creating additional macros specifying the length of each list and passing those in as well
  2. using a delimiter between lists

Neither is very difficult, but I would think there's a simpler way to do this.

I'm using Stata 13 for Windows.


Solution

  • The following works for me:

    program define myprog
    syntax, list1(string) list2(string)
    
    display "`list1'"
    display "`list2'"
    end
    
    local lista "a b c d"
    local listb "e f g h"
    
    myprog, list1(`lista') list2(`listb')
    

    or:

    capture program drop myprog
    program define myprog
    
    tokenize `0', parse(";")
    
    local list1 `1' // optional
    local list2 `3' // optional
    
    display "`list1'" // or "`1'"
    display "`list2'" // or "`3'"
    end
    
    local lista a b c d
    local listb e f g h
    
    myprog `lista';`listb'