0

I'm using the split command to split a variable which has multiple strings separated by a semicolon. I would also like to keep track of how many new string variables have been created as a consequence of splitting the original string variable and store it in a local macro.

So for example, if my initial data is something like:

State

PA;CA
MA
WA;CA;OR

And I use split State, p(;):

State     State1 State2 State3

PA;CA      PA     CA
MA         MA
WA;CA;OR   WA     CA     OR

I would like to be able to find that it has created 3 new variables and store that value in a local macro.

Is there a way to do this?

  • If you found our answers helpful, please consider up-voting them using the upper arrow. –  Mar 13 '19 at 20:23

2 Answers2

1

More generally, you can use the ds command:

clear

input str10 State
"PA;CA"
"MA"
"WA;CA;OR"
end

split State, p(;)
drop State

ds State*
State1  State2  State3

return list

macros:
            r(varlist) : "State1 State2 State3"

local states : word count `r(varlist)'
display `states'
3

Obviously, doing this here after split is unnecessary.

0

Many Stata commands will leave behind useful objects and split is no exception.

From the help file (which you can find by typing help split):

Stored results

    split stores the following in r():

    Scalars   
      r(nvars)       number of new variables created
      r(varlist)     names of newly created variables

These could be used like this:

clear

input str11 states
"PA;CA"
"MA"
"WA;CA;OR;PA"
end

compress
split states, parse(;) gen(S)

display `: word count `r(varlist)''
display r(nvars)

The second is probably easier.

dimitriy
  • 9,077
  • 2
  • 25
  • 50