Announcement

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

  • String variable related commands

    Hi,

    I have two queries regarding the following string variables in my database:

    1. String Variable #1: Location of Export (- this variable contains values such as Singapore, US, India, Ghana etc.)
    I have created a separate mapping file that maps individual countries to regions (eg. Singapore = Asia, US = North America, India = Asia, Ghana = Africa etc.)
    I was wondering if there is any elegant way for me to replace the country names in "Location of Export" with the corresponding regions.

    2. String Variable #2: Export Share (- this variable contains the absolute amount in USD million and the % share eg. 132.8 (54.0%))
    Is there any way for me to extract only 54.0 from the above variable. I do not need the rest since I am working with only the % exports.

    Thanks very much.

  • #2
    #1 I don't understand.

    #2 Almost certainly, someone will show you a solution based on regular expressions, and that's more than fine. Here is a solution based on the other string functions (NB: help string functions documents many useful tools) as well as commands for string variables.


    Code:
    clear 
    set obs 2 
    gen test = cond(_n == 1, "132.8 (54.0%)", "666.6 (42.1%)")
    gen wanted = substr(test, strpos(test, "(") + 1, .)
    destring wanted, ignore("%)") replace
    list
    
         +------------------------+
         |          test   wanted |
         |------------------------|
      1. | 132.8 (54.0%)       54 |
      2. | 666.6 (42.1%)     42.1 |
         +------------------------+

    Comment


    • #3
      Here's the regex version:
      Code:
      gen wanted = regexs(1) if regexm(test, "\((.*)%\)")

      Comment


      • #4
        The first question, as I understand it, can be solved by a simple m:1 (or 1:1, depending on the format of your master dataset) merge. Just merge the master dataset with the mapping dataset using the country names as the key variable, then replace the country names with the newly added region names.

        Comment


        • #5
          Thanks Nick! Really appreciate your prompt feedback.
          Last edited by Deepika Deshpande; 28 Mar 2021, 19:55.

          Comment


          • #6
            Thanks Wouter (#3)! The code worked!

            Comment


            • #7
              ThanksAli (#4.) I will try the merge command. I think it should work.

              Comment

              Working...
              X