Announcement

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

  • Determining if a variable is string or numeric

    I have a set of variables, some of which are string and some numeric. Here is a view of a small number of them.

    Code:
    FDC_ACCU_dose   byte    %10.0g                
    FDC_BENICAR_d~e byte    %10.0g                
    FDC_BISOP_HC~se str6    %6s                   
    FDC_ENAL_HCT~se byte    %10.0g                
    FDC_HYZ_dose    byte    %10.0g                
    FDC_LIS_HCTZ~se str7    %7s                   
    FDC_TRIA_HCT~se str7    %7s
    I'd like to loop through these variables using foreach and perform a recode on the numeric variables. I tried determining if the variable was string or numeric by looking at its type. However, this is not elegant because some of the numeric variables are integers, some float etc hence there are different types for different kinds of numerics, and the string variables have different lengths, hence type is different from variable to variable there too. Is there some quick way to distinguish numeric from string variables? It occured to me to do some simple numeric operation, e.g.

    Code:
    var * 1
    which would fail for string variables and allow me to skip over them, but I must be missing something.
    Richard T. Campbell
    Emeritus Professor of Biostatistics and Sociology
    University of Illinois at Chicago

  • #2
    Code:
    ds, not(type string)
    will list all variables that are not string variables (str# or strL). Subsequently you may use the variables in r(varlist) to recode the variables you want to recode.

    Comment


    • #3
      Oh my, I had completely forgotten that command. Thanks.
      Richard T. Campbell
      Emeritus Professor of Biostatistics and Sociology
      University of Illinois at Chicago

      Comment


      • #4
        Another possibility is -confirm numeric variable x-, This would not be preferred in the current context, but it's nice to have it in the pocket for other situations.

        Comment

        Working...
        X