08-14-2014 02:55 PM
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?
Should I clarify?
I appreciate your time!
08-15-2014 09:23 AM
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?
08-15-2014 09:32 AM
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 */
/* do numeric check */
set sashelp.vcolumn (where=(libname="YOUR_LIB" and memname="YOUR_DATASET"));
call execute('%temp (DS='||strip(memname)||',Vname='||strip(name)||',Type='||strip(type)||');');
08-15-2014 09:36 AM
Or even better:
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)||'=.');
if type='char' then call execute(' and '||strip(name)||'=""');
else call execute(' and '||strip(name)||'=.');
if last then call execute(' then result=1; else result=0; run;');
08-15-2014 10:38 AM
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.
.D = "Don't Know"
.R = "Refused"
.N = "Not Answered";
proc print data=temp;
format x y z mymissing.;
08-18-2014 11:19 AM
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...
08-15-2014 11:28 AM
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.
08-18-2014 10:52 AM
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!!
08-18-2014 11:16 AM
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.
08-18-2014 11:25 AM
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.