0
votes

I found this curious behavior in the input command for Stata.

When you pass a local macro as an argument either for one variable or multiple, the input command gives this error:

'`' cannot be read as a number

Here are two examples that give the same error:

clear 
local nums 1 1 1
input a b c
    `nums'
end


clear
local num 1
input a b c 
    1 1 `num'
end

Is there a way to pass macros into the input command?

2
What an interesting idea, and one I haven't seen proposed before. Sorry to say that although Stata's documentation (in [U] 18) claims that macros can be used "anywhere in Stata", they overstated their case, unless you split hairs and claim that the information fed to the input command is data rather than Stata, and thus not "in" Stata. I suspect that whatever you are trying to do by using macros in your input data could be accomplished similarly by other means.user4690969

2 Answers

1
votes

This does not pass a macro to the input command per se, but it does achieve your desired result, so perhaps this can help with what you are trying to do?

General idea is to set the value of a variable to a local, then split the local (similar to the text-to-column button in Excel).

clear 
local  nums "1 1 1"
foreach n of local nums {
    if "`nums_2'" == "" local nums_2 "`n'"
    else local nums_2 = "`nums_2'/`n'"
}

set obs 1
gen a = "`nums_2'"
split a, parse("/") gen(b) destring
drop a
1
votes

This is in substance largely a comment on the answer to Aaron Wolf, but the code makes it too awkward to fit in a physical comment.

Given stuff in a local, another way to do it is

clear 
local num "1 1 1"
set obs 1 

foreach v in a b c { 
   gettoken this num : num 
   gen `v' = `this'
}

Naturally, there are many ways to get 1 1 1 into three variables.