Announcement

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

  • Query in STATA

    Hi,

    I have values in my variable (tax_threshold) with many "0" values. I want to replace all the "0" with missingvalue1, missingvalue2, missingvalue3...etc. How can I do it? I have tried many commands but I either get a "type mismatch" error, or other errors.

    My main goal is to convert my data set from long to wide but STATA does not let me do it and gives error if there are missing values or many values which are same. The "missingvalue"s you see under rate, I created them in excel but it takes a long time.

    My data set looks something like this:

    Country ID rate tax_threshold
    Albania 1 0 30000
    Albania 2 .13 150000
    Albania 3 .23 0
    Albania 4 missingvalue1 0
    Albania 5 missingvalue2 0
    Albania 6 missingvalue3 0
    Albania 7 missingvalue4 0
    Albania 8 missingvalue5 0
    Albania 9 missingvalue6 0
    Albania 10 missingvalue7 0
    Albania 11 missingvalue8 0
    Albania 12 missingvalue9 0
    Albania 13 missingvalue10 0
    Albania 14 missingvalue11 0
    Albania 15 missingvalue12 0
    Albania 16 missingvalue13 0
    Albania 17 missingvalue14 0
    Albania 18 missingvalue15 0
    Algeria 1 missingvalue16 10000
    Algeria 2 missingvalue17 30000
    Algeria 3 missingvalue18 120000
    Algeria 4 missingvalue19 0
    Algeria 5 missingvalue20 0
    Algeria 6 missingvalue21 0
    Algeria 7 missingvalue22 0
    Algeria 8 missingvalue23 0
    Algeria 9 missingvalue24 0


    I want the data set to look like this:

    Country Rate1 Tax_threshold1 Rate2 Tax_threshold2
    Albania
    ​​​​​​Algeria

    I will really appreciate if someone can help me. Thank you.
    Last edited by Sana Elahi; 11 Mar 2019, 12:32.

  • #2
    Welcome to Statalist.

    Perhaps the following sample code will start you in a useful direction. I reduced the number of id's in your example data to 4 so the listing wouldn't be too wide to read.
    Code:
    // create example data
    
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 country byte id double rate long tax_threshold
    "Albania"  1   0  30000
    "Albania"  2 .13 150000
    "Albania"  3 .23      0
    "Albania"  4   .      0
    "Algeria"  1   .  10000
    "Algeria"  2   .  30000
    "Algeria"  3   . 120000
    "Algeria"  4   .      0
    end
    
    // demonstrate reshape using example data
    
    reshape wide rate tax_threshold, i(country) j(id)
    list, clean
    Code:
    . list, clean
    
           country   rate1   tax_th~1   rate2   tax_th~2   rate3   tax_th~3   rate4   tax_th~4  
      1.   Albania       0      30000     .13     150000     .23          0       .          0  
      2.   Algeria       .      10000       .      30000       .     120000       .          0

    Comment


    • #3
      Thank you so much! It worked.

      Comment


      • #4
        Hi,

        I want to merge two data sets one with Country names only and the other with country names and country codes and region codes, what the most efficient way to do it? Do I have to install any library? Thank you.

        Best Regards,

        Comment


        • #5
          Your question is no longer concerned with your original topic, but is now about merging.

          You should review the output of help merge to understand how merging is done in Stata. If that isn't sufficient to resolve your questions, you should post a question as a new topic with an informative title that will draw more members who can help you, who may not be following this topic.

          Also, please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

          Comment

          Working...
          X