Announcement

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

  • saving string values in local while string values containint `'

    Hi,
    I want to save specific Country names in a local since I´m using the specific subset regulary.
    However, somehow I´m not able to save the names.
    Code:
    local country `" "Spain" "Saudi Arabia" "United Arab Emirates" "Switzerland" "Romania" "Qatar" "Portugal" "Poland" ///
    "Korea" "Kuwait" "Cote d`Ivoire" "Croatia" "Cuba" "Cyprus" "'
    I thought the /// are maybe a Problem, however, in one line it also did not work.
    Is the Cote d`Ivoire problematic? And if yes how to solve it?
    Or am I making an easy mistake?
    Thanks in advance!

  • #2
    The single left quote is problematic as it causes Stata to expand a local macro. You could fix this by preceding the single left quote with the backslash (escape) character. But this fix is likely to fire back later. I would change the single left to a single right quote, i.e. instead of Cote d`Ivoire write Cote d'Ivoire.

    You will also have to remove the line break.

    Best
    Daniel

    Comment


    • #3
      Thanks!
      Very good Workaround! helped a lot
      Maybe you can help me with something else.
      After saving the local I tried to use it in the follwing way
      Code:
      gen donor = 1 if country == `International_organizations'
      However Stata is giving me the follwoing error message
      invalid '"Saudi Arabia'
      r(198);
      Could you may give me a hint what is wrong?
      Thanks

      Comment


      • #4
        gen donor = 1 if Country == `country'
        sorry thats the actual code

        Comment


        • #5
          Hello Jay,

          If County is a string variable, and you're comparing it to a string value in local country, then you need quotations. E.g.:
          Code:
          gen donor = 1 if County =="`country'"

          Comment


          • #6
            With this code Stata will see

            Code:
            gen donor = 1 if Country == "Spain" "Saudi Arabia" ...
            Besides the fact that this is invalid syntax that could be fixed by using compound double quotes (cf Roger's suggestion), as in

            Code:
            gen donor = 1 if Country == `""Spain" "Saudi Arabia" ..."'
            what do you expect this code to do?

            Tell us more about your dataset and what you are trying to do here.

            Best
            Daniel

            Comment


            • #7
              I reread this thread and daniel klein's post and realized that my solution probably wasn't useful for this situation.

              It looks more as if you want to do something with inlist() or the like. Are you trying to flag if an observation is in your list of countries? If so, something like
              Code:
              gen donor = 1 if inlist(Country,"Spain","Saudi Arabia",etc.....)
              might actually be what you're looking for.

              Comment


              • #8
                My thoughts exactly. Note that inlist() will only accept 10 arguments when used with strings. A better approach might be to create a dataset with the desired country names then merge with the original data. Details of the solution depend on details of what exactly is given and what exactly is wanted.

                Best
                Daniel

                Comment


                • #9
                  Thanks!
                  I would like to generate a new variable called donor
                  Code:
                  gen donor = .
                  and replace it if the Country Name is in the local I´ve defined.
                  Yes I see the gen command was wrong, but even if I replace it by replace and generate the variable donor before, it does not work out.
                  The Full Country list in the local also contains some "-" between names and brackets () with abbreviations. However, I thought that that should not matter.
                  Anyways, thank you very much for your help so far!

                  Comment

                  Working...
                  X