Announcement

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

  • changing the format of the data

    The two variables in my panel data-set, "index" and "year" are being identified as "long" type data but I want to convert them into "double" format. I tried using the two commands:

    destring index, replace
    index already numeric; no replace

    encode index, gen (in2)
    not possible with numeric variable

    But none of them worked well. How can I change the format of these two variables ? TIA.
    Attached Files

  • #2
    You would need to copy the data into a new variable, as below. Naturally, you would need to add a variable label, and then reorder, drop the old variable and rename the new variable, if you wish.

    Code:
    gen double newindex = index
    But I don't see why you need double precision for what appear to be integers with an already adequate data storage type.

    Comment


    • #3
      (crossed in the ether with Leonardo; somewhat redundant)

      Changing the data type of a variable can be done with the -recast- command, but I don't think there's a reason to do that. A change from long to double is a change in type, not a change in format. "Format" refers to how a variable is displayed; "type" refers to how it is stored in the computer. I'm assuming you intended a change of type, and that's not necessary here. A variable of type "long" can easily hold a year value (see -help data types-). In fact, an "int" would be sufficient. Similarly, generalizing from the values you show for index indicates that a long would be fine: double would be useful if your index values were greater than 2,147,483,62.

      Further, the screenshot that you posted (check the FAQ, you won't want to use a screenshot in the future) show index and year in blue, indicating a display of value labels of variables, not values. It's unlikely you want values labels for this variable, which you likely inadvertently did by mistakenly using -encode-. Read the help about value labels vs. values, and read again about the difference in *purpose* between -encode- and -destring-

      If all you want is for your values to display differently, changing their types won't help. For that purpose, see -help format-.

      Finally, saying "none of them worked well" doesn't give us information with which to help you. We don't have a way to know what "worked well" means to you.
      Last edited by Mike Lacy; 05 Jul 2019, 12:37.

      Comment


      • #4
        I tired using this code:
        gen double newindex = index. But it does not create an exact copy of the variable "indeX" rather some different numbers are generated. I have attached the screenshot.
        Attached Files

        Comment


        • #5
          Per my posting, you have a confusion about values vs. value labels, and have been led into confusion by what -encode- did in creating newindex. -encode- assigned consecutive integers to the string values of such things as "11704" based on their string sort order, and then assigned those original string values as value labels to those integers. I'd emphasize again that you'll benefit from reading the help on -encode-, concentrating first of all on what its purpose is as opposed to that of -destring-. I quote from the manual text for encode "Do not use encode if varname contains numbers that merely happen to be stored as strings." When you are confused, just reading the first line help is often not sufficient, and going into the explanatory depth of the PDF manual is necessary. If you haven't taken a look at that yet, you should do so, and follow up with any questions.

          Comment


          • #6
            Please do follow the advice in the FAQ and suggested by Mike in #3. Use -dataex- to provide us with a usable example of your data and post it here. Also include a more precise description of what you want the data to look like and if you really do mean converting between data types (see -help datatype-) or formatting the display of numbers (see -help format-).

            edit: crossed in the ether again with Mike.

            Comment

            Working...
            X