Announcement

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

  • Major problems using recast to change variable type. Please help!!

    Hell guys,

    I am having a strange problem. I am working on a project that needs to perform logistic regression. I went Statistics > Binary Outcome > Logistic Regression to call up the window to do the logistic regression. When I try to select the dependent variable or independent variables in that window only 3 variables show in the selection despite there are 8 variables in the data set. I looked at the variable type and looks like the the ones recognized by the logistic regression are the ones with byte as variable type. All variables not recognized have string as variable type.

    So I try to changes the variables type from string to byte to get those variables recognized. The command recast looks like the one that I need with a simple syntax so I entered:

    recast byte Race

    The return:

    Race: byte invalid

    This problem continues to happen all variables. Please help. Thank you.

  • #2
    Code:
    recast
    won't recast string to numeric. You should try encode.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      recast
      won't recast string to numeric. You should try encode.
      Thank you so much. I feel so silly. Now the logistic regression windows recognize the new variable. Recast now works on the new variable as well. Great.

      Comment


      • #4
        I looked at the help and manual entry for recast and do think this should be explained more directly.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          I looked at the help and manual entry for recast and do think this should be explained more directly.
          I am very new to stata and mostly self learned so this has been challenging. Just to confirm, so there is no other way to change a variable from string to numeric unless you use encode to create a new variable? When I import my data from excel to stata some variables are recognized as numeric and some variables are recognized as string (regardless of whether the actual values are strings or numbers) creating this problem. Is there a more efficient way to do this or everyone has to use encode to change variables to the correct type after importing? Thank you so much.

          Comment


          • #6
            No; there's
            Code:
             destring
            -- which (modesty aside) I first wrote....

            But the real issue is -- sure, you're new and it's all very confusing -- how are you trying to learn? because the opening section of the help for encode points you to destring.

            encode creates a new variable named newvar based on the string variable varname, creating, adding to, or
            just using (as necessary) the value label newvar or, if specified, name. Do not use encode if varname
            contains numbers that merely happen to be stored as strings; instead, use generate newvar = real(varname)
            or destring; see real() or [D] destring.

            I started using Stata in 1991 when the manuals (a long one for Stata 2.0 and a short one for 2.1) were about 700 pages long and I could -- and sometimes did -- read them in bed, in the bath or on the train. That is no longer possible, but the sense I get is that people new to Stata just think Google -- Google -- Google.

            That's like going to the library when you own the key book and it's on your desk. The key documentation is bundled with Stata.

            My advice is -- read the help for each command; look at the links provided -- read the manual entries.

            Comment


            • #7
              Hello Nick,
              I am trying to dealing with the following problem:
              There is a numeric variable in my dataset which is double precision. This numeric variable must be converted to string type for my next work.
              I tried to use "tostring" code to convert this numeric variable to string variable. But failed. The "recast" can not change this variable from double precision to float/int /long/byte. Then I used "decode" code to generate a new variable based on this numeric variable. But there is no value of the new variable.
              Could you have some suggestion for me to deal with this problem? Thank you

              Duo Liu

              Comment


              • #8
                Duo Liu You also posted this question at https://www.statalist.org/forums/for...ode-don-t-work Please don't try to run two or more threads on the same topic.

                Anyone else interested: Please follow that thread where there is already a first answer.

                Comment

                Working...
                X