Announcement

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

  • Last two digits

    I am working with the PUMS data. I would like to create a dummy variable that would indicate if the last two digits of PUMA variable (which is a 5 digits variable) is 00. How do I do that? Thanks!

  • #2
    It is not a good idea to use abbreviations like PUMS on this forum. It is a multi-disciplinary, international forum, and abbreviations that are widely known in your own field may be completely incomprehensible to others who are nevertheless able and eager to help you with the Stata aspects of your problem. You just delay getting a response, and reduce the probability of even getting one at all, when you do this. You should only use abbreviations that would be reasonably understood by all educated people, regardless of where they are or what they do.

    You also don't provide enough information to answer your question simply. It depends on whether or not your PUMA variable is a string variable or a numeric variable. The simplest case is numeric:

    Code:
    gen byte ends_in_double_zero = (mod(PUMA, 100) == 0)
    If it's a string variable, and if all of its values are 5 digits long (so no leading zeroes were omitted), then it's
    Code:
    gen byte ends_in_double_zero = (substr(PUMA, 4, 2) == "00")
    Finally, if it's a string variable but some of the values have fewer than 5 digits, then you could do:
    Code:
    gen byte ends_in_double_zero = (substr(reverse(PUMA), 1, 2) == "00")
    By the way, it is the custom in this community that we use our full real first and last names, to promote professionalism and collegiality. Please do likewise. Unfortunately, you cannot edit your username in your profile. But if you click on "CONTACT US" you can message the Forum administrator with a request to make the change, and he will do it for you. Thank you.

    Comment


    • #3
      Dear Clyde,

      could you tell me how I would have to apply your example for numerical numbers, if the length of my variable was to vary?

      I want to extract the last two digits of a numerical variable that has 1 to 7 digits, and create a new variable out of it.

      Thank you very much!
      Stephanie

      Comment


      • #4
        Stephanie: The last two characters of a string variable strvar are always substr(strvar, -2, 2).

        You say your variable is numeric. Assuming that
        numvar contains integers only, then

        Code:
        mod(numvar, 100)
        always returns the last two digits, regardless of the length (number of digits, presumably).

        Comment


        • #5
          Thank you for your quick and helpful reply!

          Comment


          • #6
            Hi all,

            I have a similar question. I have an (independent) variable 'yearquarter' in my data that looks like "2001q3" and so on.

            After performing regression, I have to generate a graph with the Price as dependent variable on the Y-axis and Yearquarter on the X-axis using "margin and marginsplot".
            But in order to do so, I want to only keep the last digit of the variable yearquarter, so in this case the "3".

            How can I only keep this last digit?


            Thanks!!


            Comment


            • #7
              Knowing what your variable looks like isn't enough information. It might be a string variable, or it could be a real Stata internal format quarterly date variable with an appropriate %tq format applied. If it's the former, the first step is to create the latter by using the -quarterly()- function. From there you can extract the quarter (1, 2, 3, or 4) alone using the -quarter()- function. Worse yet it might be some other kind of numeric variable with value labels that look like "2001q3" applied--in which case you've got a fair amount of work to do to fix that!

              In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

              When asking for help with code, always show example data. When showing example data, always use -dataex-.

              Comment

              Working...
              X