Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Expand command for string variables

    Hello everyone!

    I require assistance for expand command.

    My data consists of the following in respect of US states.
    state name, year, month of the year, product description code, and monthly tax rate.
    There are:
    45 states
    6 years,
    12 months for each of the year
    15 products

    I want to add two new states to the variable "state name" in my data set using the expand command so that they have a similar observations structure to the existing 45 states, and I can then make changes.

    I studied the stata manual. I understand that expand command will not work for string variables like "state name." But not sure how using the destring command would help me.

    Thank you

  • #2
    Given that this question did not provide any sample data, I'll just suggest the most basic way:
    1. Save the data set, let's call that state_data.dta
    2. Use keep to keep data from any two states. For example: keep if inlist(state, 1, 2) OR if your state is string: keep if inlist(state, "Alabama", "Arkansas"). Make sure they are both complete, with 1080 cases each, totally 2160 cases.
    3. Rename state 1 to the first additional state, and state 2 to the second additional state using replace, like: replace state = 53 if state == 1, OR replace state = "Puerto Rico" if state == "Alabama"
    4. Add the new information for the two new states
    5. Use append to add the original data back. E.g. append using state_data
    Last edited by Ken Chui; 09 Mar 2023, 06:17.

    Comment


    • #3
      Thank you so much for your prompt help. The code worked.

      Comment

      Working...
      X