Announcement

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

  • How can I convert numeric label values to actual variable?

    I am currently using stock market data and one of my variables of interest contains numeric label that is actually the adjusted close stock values that I need to use.
    Is there anyway that I can convert those numeric values in label to actual variable?

    (Ex: One of the values in my variable is 1168 and label of 1168 is 10997.93796. And I need to use 10997.93796 not 1168 to run my analysis.)

    Thank you in advance!!

    Best,

    Hanna

  • #2
    Hanna: welcome to Statalist. I recommend that you read the section about extended functions in the [P] macro entry in the Manuals. You can follow the links from help macro. These functions allow you to extract this sort of information, I think.

    Please read the Forum FAQ (black bar at top of page). Amongst other things, Forum etiquette is to post using real names (firstname lastname). So, please re-register to do this, by hitting the "Contact Us" link at bottom right of the page, and make your request (easy and fast). It'll increase your chances of getting helpful responses in future.

    Comment


    • #3
      Aside from the question why you have a situation like this, and whether it is the result of something that went wrong in the first place, Stephen points into the right direction. Programming this from first principles should not be too complicated, and here is a sketch

      Code:
      // get the values of <varname>
      qui levelsof <varname> ,l(lvls)
      
      // create recoding rules
      foreach lev of loc lvls {
          loc lbl : lab (<varname>) `lev'
          loc rules `rules' (`lev' = `lbl')
      }
      
      // create new variable
      recode <varname> `rules' ,g(<newvar>)
      The idea here is to loop over the values of a variable, get its labels (which should be another value) and put them together into a local that can be passed to recode later.

      Best
      Daniel
      Last edited by daniel klein; 14 Nov 2014, 04:30. Reason: bugfix in the code

      Comment


      • #4
        These solutions seem rather indirect. Isn't this a case for decode followed by destring?

        Comment


        • #5
          These solutions seem rather indirect. Isn't this a case for decode followed by destring?
          Much better! Sometimes I seem to completely miss the straight forward solutions ...

          Code:
          decode <varname> ,generate(newvar)
          destring <newvar> ,replace
          Best
          Daniel

          Comment


          • #6
            Click image for larger version

Name:	Screen Shot 2020-04-05 at 12.40.55 AM.png
Views:	1
Size:	43.6 KB
ID:	1544797



            Hello,

            If I have a labeled ordinal variable in blue which looks like this:

            Strongly agree (1)
            I somewhat agree (2)
            I somewhat disagree (3)
            I strongly disagree (4)
            Don't know (.d)

            Only when I click on each observation, I can see the value label that is attached.

            Is it possible to take those labels and directly turn into a numeric variable that can be used for analysis?


            Thanks

            Comment


            • #7
              #6 You already have that numeric variable. That is how value labels are possible.

              Comment


              • #8
                Thanks for your reply. I was referring to taking an entire variable from blue, which has both the observation and value label, to the standard black, which only has the numbers.

                Comment


                • #9
                  if you look at the help file, you will see that there is a -label drop- command which appears to be what you want; see
                  Code:
                  help label

                  Comment

                  Working...
                  X