Announcement

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

  • Unsing a (value label) to generate a new numeric variable

    Hi,

    I have a numeric variable (ID) in a numeric long format with 10 digits only which has a value label called (ID_label).

    Is there any way to generate a "new numeric long formatted variable" named (ID_new) = (ID_label)?
    Meaning, I would like to generate a new variable using the values label of another existing variable.





    example of my data set:


    ID_nolabel | ID | ID_new

    1 |1234567890 |1234567890
    2 |2345675678 | 2345675678
    3 |5566778899 | 5566778899
    4 |45678901234 | 45678901234
    5 |1234098765 | 1234098765



    Any guidance would be appreciated
    Thank you
    Last edited by rena jk; 01 Aug 2023, 08:41.

  • #2
    decode may be what you are looking for.

    Comment


    • #3
      It's not entirely clear what you have from your description. You should have posted this example data using the -dataex- command so that we would know exactly what is going on. I'll make an educated guess as to what's going on. If I'm wrong, my solution will not work for you. In that case, when you post back, be sure to use -dataex- to show example data.*

      I'm guessing that the variable ID is a variable whose actual values are 1, 2, 3,... and which has attached to it as value label so that when you -browse- or -list- it you see values like 1234567890. You want a new numeric variable whose actual values are 1234567890.

      Code:
      decode ID, gen(ID_new)
      destring ID_new, replace
      Note: The variable ID_new will emphatically not be a long variable. The long storage type is not large enough to accommodate 10 digit numbers. ID_new will be a double. Don't try to recast it to a smaller data type--if you do, you will lose low order digit precision.

      *If you are running version 18, 17, 16 or a fully updated version 15.1 or 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.
      Last edited by Clyde Schechter; 01 Aug 2023, 09:40.

      Comment

      Working...
      X