Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8958717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:16:42+00:00 2026-06-15T15:16:42+00:00

In Stata, I have a set of variables that all begin with pkg .

  • 0

In Stata, I have a set of variables that all begin with pkg. In their current state, their endings are numeric: pkg1, pkg2, pkg3, pkg4 and so on.

I need to change all of these variables’ endings to strings: pkgmz, pkggmz, pkgsp, pkgsptc etc.

I have a column of these string endings, which I can designate as a local list.

For example:

local croplist mz gmz sp sptc mil cof suk tea ric

How do I change the numeric endings to the string endings?

My guess at the code can be found below and the ??? indicate where I am stumped:

local croplist crops mz gmz sp sptc mil cof suk tea ric

foreach x of varlist pkg* {
    local new1 = substr(`x', 1, 3)
    local new2 = ???
    rename `x' ``new1'`new2''
    label var ``new1'`new2'' "Avg district level `new2' price"
}

I wonder if it would be better to utilize the regexr() command, but can’t think of a way to include it.

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:16:43+00:00Added an answer on June 15, 2026 at 3:16 pm

    There is no need here to invoke regular expressions. You have the new suffixes; the prefix pkg is always the same, so the labour of extracting it repeatedly is unnecessary. The heart of the problem is cycling over two lists at once. Here is one way to fix your code.

    
    local croplist mz gmz sp sptc mil cof suk tea ric
    local j = 1 
    foreach x of varlist pkg* {
        local sffx : word `j' of `croplist' 
        rename `x' pkg`sffx'
        label var pkg`sffx' "Avg district level `sffx' price"
        local ++j 
    }
    

    Note also rename in Stata 12+ can handle this; regexr() is a function, not a command; a more general discussion in http://www.stata-journal.com/sjpdf.html?articlenum=pr0009 (a little out-of-date, but relevant on the main issue); you have too many quotation marks on your rename command, so it wouldn’t work.

    EDIT 30 July 2018

    I tend now more often to use gettoken:

    local croplist mz gmz sp sptc mil cof suk tea ric
    foreach x of varlist pkg* {
        gettoken sffx croplist: croplist
        rename `x' pkg`sffx'
        label var pkg`sffx' "Avg district level `sffx' price"
    }
    

    The local macro croplist is a stack. Each time around the loop we take the top item from the stack and leave the rest for the next time.
    Each time around the loop

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, i have to state that i am new to Java in
I have a range named STATE. I want to set data validation in range
I have state data in a txt file that I used to seed my
basically i have (state, state code) pairs, that are subsets of country [USA] ->
I have a State class that includes configuration as well as some state-specific methods.
Before I ask the question let me state that I have attempted to google
A database connection on Oracle can have session state that persists for the lifetime
This is what I have: All objects that can be persisted on the database
So .... There are obviously many questions that all have been asked about Singletons,
I have a requirement of tracking changes to some set of variables in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.