Announcement

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

  • replace string variable in terms of its range

    Hi, I have string variable bostonid, and I want to simplify them into containing either "boston, vir, or slc." Because of the large numbers (1980 observations), I sorted bostonid, and tried to replace them all with "boston" for in range of 1 to 56.

    Originally posted by Joseph Coveney View Post
    It's not what I would recommend for the coordinates, but if you insist on keeping them as strings, then you'll either need to enclose the numerals within double quotation marks or else use the string() function.
    Code:
    replace x = "-43.027524" if house_num == "700"
    replace y = string(120.045524,"%10.6f") if house_num == string(700, "%3.0f")
    There are good reasons for keeping an identifier variable as a string, but you might want to consider using the destring command for the coordinates and working with them as numeric.
    I tried this but getting errors. Anyone knows what went wrong?
    Attached Files

  • #2
    It seems you are opening new threads for the same topic.

    Below, I share the link of some of them:

    http://www.statalist.org/forums/foru...ring-variables
    http://www.statalist.org/forums/foru...-combine-merge
    Last edited by Marcos Almeida; 26 Jun 2016, 07:54.
    Best regards,

    Marcos

    Comment


    • #3
      Let me repeat the advice I gave on one of the earlier threads Marcos linked to.

      Please 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. See 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, as described in section 12 of the FAQ. Nobody wants to spend the time retyping material from your pictures when we try to assist you, and that reduces the number of readers willing to assist.

      What I didn't see in your output is trying help string functions and reviewing the advice given there. You will see that the string function takes two arguments, the first of which is a number, or a variable containing numeric values. I see no evidence that you have a variable named boston for\ your first four examples. But in the final example, where your string function is used as the documentation suggests, you are done in by if bostonid in 1/56 which does not mean what you think it does. Those clauses attempt to limit your replace command to observations 1 through 56 where the variable bostonid can be evaluated as "true" which to Stata is a non-zero numeric value. But bostonid is a string, so you get a type mismatch.

      Your misunderstanding of the if and in clauses is fairly basic and suggests you need a better introduction to using Stata. When I began using Stata in a serious way, I started by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through Stata's Help menu. The objective in doing this was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax.

      Added in edit: I see that the questions were addressed by Nick Cox at one of the other posts; my advice on further reading however still stands. Stata provides excellent documentation that repays the effort spent in reading it.
      Last edited by William Lisowski; 26 Jun 2016, 09:41.

      Comment

      Working...
      X