0
votes

I learned that to change lower case variable names to upper case variables I need to do the following:

foreach var of varlist * {
  rename `var' `=upper("`var'")'
}

But I can't comprehend how this can really work.

First, rename does not require = to change variable names.

Second, I understand that I need to embrace var with ` and '

But what does that ` and ' mean surrounding

=upper("var'")

?

1

1 Answers

1
votes

You don't need to do that. You don't need a loop and you don't need that syntax. Consider

. sysuse auto, clear
(1978 Automobile Data)

. ds
make          mpg           headroom      weight        turn          gear_ratio
price         rep78         trunk         length        displacement  foreign

. rename *, upper

. ds
MAKE          MPG           HEADROOM      WEIGHT        TURN          GEAR_RATIO
PRICE         REP78         TRUNK         LENGTH        DISPLACEMENT  FOREIGN

Otherwise you are puzzled at the

`= ' 

because indeed that is nothing to do with rename. That syntax obliges Stata to evaluate a scalar expression on the fly so that rename sees only the result of that expression. In your case the string expression

upper("`var'") 

yields an upper-case version of the variable name contained in local macro var.

This syntax is documented at help macro and [P] macro (e.g. in this version p.13) as one kind of expansion operator.

All that said, all variable names upper case is horrible style....