0
votes

I would like to add a prefix or suffix to all variables which do not begin with x:

clear
input xa xb c d
1 1 1 1
end

I thought something like this: rename (!x)* y* which should give this result: xa xb yc yd. Thanks for any hint.

2

2 Answers

2
votes

The foreach loop can be avoided

. rename (`r(varlist)') (y=)
1
votes

This is not ideal in my mind, but off the top of my head, perhaps something like renpfix might be of use to you:

. clear
. input xa xb c d
            xa         xb          c          d
  1. 1 1 1 1
  2. end
. list

     +-----------------+
     | xa   xb   c   d |
     |-----------------|
  1. |  1    1   1   1 |
     +-----------------+

. renpfix "" y
. renpfix yx x
. list

     +-------------------+
     | xa   xb   yc   yd |
     |-------------------|
  1. |  1    1    1    1 |
     +-------------------+

Here, I've first added a prefix to all the variables, thus resulting in your first two variables having a prefix of yx; then I've replaced the yx prefix to get back to x for the first two variables.

Update

Another option is to use ds or findname (install using search findname) and using the not argument to negate the matched variables

. clear
. input xa xb c d
            xa         xb          c          d
  1. 1 1 1 1
  2. end
. list

     +-----------------+
     | xa   xb   c   d |
     |-----------------|
  1. |  1    1   1   1 |
     +-----------------+

. ds x*, not
c  d

. foreach v in `r(varlist)' {
  2. rename `v' y`v'
  3. }
. list

     +-------------------+
     | xa   xb   yc   yd |
     |-------------------|
  1. |  1    1    1    1 |
     +-------------------+

ds and findname store their results in `r(varlist)', so you can use foreach after identifying the relevant variables.