Efficient way to convert numeric variables to alphanumeric?

Reply
Frequent Contributor
Posts: 131

Efficient way to convert numeric variables to alphanumeric?

Hello all:

I am recoding a medium sized set of variables to include specific valid values for when the data should be missing, etc.  I'm wondering, now, whether there is an efficient way to convert my numeric variables so they're alphanumeric?


Any ideas?

Should I clarify?

I appreciate your time!

Thank you!

Grand Advisor
Posts: 17,338

Re: Efficient way to convert numeric variables to alphanumeric?

Clarify, an example is helpful.

The put function works well in most cases, so where do you need efficiency?

Grand Advisor
Posts: 10,210

Re: Efficient way to convert numeric variables to alphanumeric?

Or a set of custom formats may be more efficient then creating new variables. Examples of what you are trying to accomplish might help.

Frequent Contributor
Posts: 131

Re: Efficient way to convert numeric variables to alphanumeric?

To clarify, I'm cleaning a dataset by including if-then logic to for when I expect data should be be non applicable, is actually missing, etc.

So, for example

if var1=" " and var2=" " then charvar=.N  (i.e. non applicable; data was not expected)

if var1=" " and var2=" " then numvar=.N

However, after doing this, I see that the places where I used this kind of code for the numeric variables, won't work because I specified creating a character valid value.  I'd still like "not applicable" valid values to display as .N for all variables.  How would you suggest I identify all the numeric variables in my dataset and convert them to alphanumeric quickly? Some macro? Use a formatting instead?

Thanks!!

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Efficient way to convert numeric variables to alphanumeric?

Sorry, maybe provide some test data an output.  Why would you need to convert them to character?  Use a conditional macro and base it off sashelp.vcolumn e.g:

%macro temp (DS=,Vname=,Type=);

     %if "&TYPE."="char" %then %do;

          /* do character check */

     %end;

     %else %do;

          /* do numeric check */

     %end;

%mend temp;

data _null_;

     set sashelp.vcolumn (where=(libname="YOUR_LIB" and memname="YOUR_DATASET"));

     call execute('%temp (DS='||strip(memname)||',Vname='||strip(name)||',Type='||strip(type)||');');

run;

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Efficient way to convert numeric variables to alphanumeric?

Or even better:

data _null_;

     set sashelp.vcolumn (where=(libname="SASHELP" and memname="CARS")) end=last;

     if _n_=1 then do;

          call execute('data want; attrib result format=best.; if ');

          if type=char then call execute(strip(name)||'=""');

          else call execute(strip(name)||'=.');

     end;

     else do;

          if type='char' then call execute(' and '||strip(name)||'=""');

          else call execute(' and '||strip(name)||'=.');

     end;

     if last then call execute(' then result=1; else result=0; run;');

run;

Frequent Contributor
Posts: 131

Re: Efficient way to convert numeric variables to alphanumeric?

I wanted to convert them to character so the .N valid values would be valid for the now numeric variables

Grand Advisor
Posts: 10,210

Re: Efficient way to convert numeric variables to alphanumeric?

If you use formats then numeric variables can be assigned values of .A to .Z and a custom format can display any text you would like to display meaning.

proc format;

value mymissing

.D = "Don't Know"

.R = "Refused"

.N = "Not Answered";

run;

data temp;

     x=.D;

     y=.R;

     z=.N;

run;

proc print data=temp;

format x y z mymissing.;

run;

Frequent Contributor
Posts: 131

Re: Efficient way to convert numeric variables to alphanumeric?

Will this way with the custom formats work if my input dataset's numeric variables do not have any character valid values as is the case? I wanted to create valid values for my numeric variables where specific missing values would show as .M, .N, etc...

Regular Contributor
Posts: 180

Re: Efficient way to convert numeric variables to alphanumeric?

How about using "Special Missing Values" see SAS(R) 9.3 Language Reference: Concepts, Second Edition.

Valued Guide
Posts: 3,206

Re: Efficient way to convert numeric variables to alphanumeric?

yep, as CTorres made the reference, you are only missing the missing statement. SAS(R) 9.3 Statements: Reference.
It will define you missing cases into dedicated missing values while leaving the real numerics untouched.

It is about Statistics, this concept is common to that.
With DBMS data-management the missing concept of statistics is missing the have an not available (not stored) record instead.

Just to give you attention to avoiding a DBMS or Proc SQL when using statistical data with multiple missing values.

---->-- ja karman --<-----
Frequent Contributor
Posts: 131

Re: Efficient way to convert numeric variables to alphanumeric?


It seems like the MISSING statement will help if my input dataset's numeric variables have character valid values which I would like SAS to recognize as special missing values. Is this true?  Because in my case, my input dataset's numeric variables all have only numeric valid values.  I want to, then, include certain if-then conditions to specify valid values such as .M and .N (dedicated missing values).  Will the MISSING statement help for this?

Thank you so much!!

Respected Advisor
Posts: 4,973

Re: Efficient way to convert numeric variables to alphanumeric?

Your original code for NUMERIC missing values was correct (assuming you add a semicolon):

if var1 = " " and var2 = " " then numvar = .N;

When referring to a special missing value, include the dot:

if numvar = .N then do;

When printing, however, the dot doesn't print.  You would get "N" as the printed value.

Your original code for CHARACTER missing values was incorrect (even if you added a semicolon).  You need to add quotes to assign a character string:

if var1 = " " and var2 = " " then charvar = ".N";

You could just as easily assign "N/A" or any other string as the value of CHARVAR.


Valued Guide
Posts: 3,206

Re: Efficient way to convert numeric variables to alphanumeric?

Well I gave the link tot the missing statement. There is an example how it works.

It is intended to be used having multiple type of missing values. Amazingly a statistical tool supporting that.
What is holding you for a try.

I mentioned it is not a well known feature,  o give attention to it when exporting it to an other storage method outside SAS.  

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 13 replies
  • 377 views
  • 0 likes
  • 7 in conversation