Announcement

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

  • Recoding numeric variable to character

    I'm need help in recoding numeric variables to characters.
    Last edited by Rohith Perike; 03 Mar 2025, 10:52.

  • #2
    That depends on the details of what you want to do exactly. For example, should all observations become strings (characters)? It is easiest to explain those details with an example. See the FAQ (black bar near the top of this page ) for how to do so.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Click image for larger version

Name:	Screenshot 2025-03-03 153127.jpg
Views:	1
Size:	16.0 KB
ID:	1773799
      I have a variable by name LOCAL_MONTH. I want to change the month numbers to actual month name (eg. May for 5, june for 6)

      Comment


      • #4
        Ah, it's better to apply a labeling scheme. Storing them as text can be inconvenient later. Here is an example (with sample data):

        Code:
        clear
        input mos freq
        5 1
        6 22
        7 101
        8 88
        9 8
        end
        
        expand freq
        
        * Your original form
        tab mos
        
        * String format (Bad idea)
        generate mos_string = "May" if mos == 5
        replace  mos_string = "June" if mos == 6
        replace  mos_string = "July" if mos == 7
        replace  mos_string = "August" if mos == 8
        replace  mos_string = "September" if mos == 9
        
        * Tabulate: notice that months are now alphabetically ordered
        tab mos_string
        Result is a bit odd:

        Code:
         mos_string |      Freq.     Percent        Cum.
        ------------+-----------------------------------
             August |         88       40.00       40.00
               July |        101       45.91       85.91
               June |         22       10.00       95.91
                May |          1        0.45       96.36
          September |          8        3.64      100.00
        ------------+-----------------------------------
              Total |        220      100.00
        Code:
        * Instead, try labeling:
        label define month_name 5 "May" 6 "June" 7 "July" 8 "August" 9 "September"
        label values mos month_name
        
        * Now the order makes sense
        tab mos
        Here is the labeled version:

        Code:
                mos |      Freq.     Percent        Cum.
        ------------+-----------------------------------
                May |          1        0.45        0.45
               June |         22       10.00       10.45
               July |        101       45.91       56.36
             August |         88       40.00       96.36
          September |          8        3.64      100.00
        ------------+-----------------------------------
              Total |        220      100.00

        Comment


        • #5
          "Friendly amendment" to #2. With only five months, I suppose writing out all five is not especially burdensome and offers the advantage of transparency. But if the full data set contains all twelve months, or a subset that is nearly that large, I think it is better to define the label in a loop:

          Code:
          levelsof mos, local(months)
          foreach m of local months {
              label define month_name `m' "`:word `m' of `c(Months)''", add
          }
          label values mos month_name

          Comment


          • #6
            For other stored sequences, look through the results of

            Code:
            creturn list
            and/or consult https://journals.sagepub.com/doi/pdf...867X0400400213

            Comment

            Working...
            X