11-26-2012 03:13 PM
I have a macro that reads in a group of files.
However, in some files all observations for 1 or more variables are missing.
If I try to apply a format (eg MMDDYY10.) to a variable with all missing values I receive an error.
Does anyone know how I can apply a format only when the variable's observations are not completely missing?
11-26-2012 03:22 PM
Does the error relate to data type such as trying to use a numeric function with a character variable? Missing values are usually not a problem but type mismatches are. In which case you would need to know whether the variable(s) were actually created as numeric or character.
11-26-2012 03:34 PM
For files where the variable is completely missing it's being stored as Char with Format $1.
In files where it's not completely missing it's stored as Num.
For the variable where I'm receiving an error I'm trying to apply MMDDY10.
11-26-2012 03:38 PM
The best solution would be finding a way to make the variable numeric, even when it is always missing.
You could modify your format statement, but you run the risk of formatting variables that you don't want formatted. For example:
format colE - numeric - colH mmddyy10.;
That will format all numeric variables only, starting with colE and ending with colH. But there may be other numeric variables in that range that aren't dates. It all depends on whether this becomes easy or not when specifying ranges of variables.
11-26-2012 03:40 PM
Sounds like a proc import problem. You cannot apply a mmddyy10. format to a character variable.
Are the ONLY variables in your file that are char and have a format of $1 variables that were imported incorrectly?
If that is the case, it would be easy to identify them and replace them with same named variables with the format you want to apply.
11-26-2012 03:52 PM
The format should be char for the variable when it is completely missing. In some files the variable's observations are completely missing (this isn't an error). What I'm asking is how do I tell SAS not to apply a format (eg MMDDYY10.) for files where the variable's observations are completely missing.
11-26-2012 04:04 PM
There are only two variables where this is an issue (ie date_returned and date_left).
How do I tell SAS to not apply MMDDYY10 when either date_returned or date_left is completely missing?
11-26-2012 04:07 PM
Do you plan to ever combine these files into one?
You won't be able to if you leave one character in one dataset and one numeric in the other.
Its also much easier to correct the code to convert date_returned and date_left (which are dates into numbers) to a number than it is to check if all is missing.
You need to have the dataset created first. Then need to check if all are missing or if the variable is character. If it isn't character then apply the format, if it is don't. But this will have to be macro code because formats aren't conditional statements so you can't put it in a standard dataset.
11-26-2012 04:27 PM
Reeza raises a good point about whether you need to combine the files.
But if you need to apply this to an individual file, take two steps. First, remove date_returned and date_left from the FORMAT statement that you have now. Then add these two statements:
format date_returned - numeric - date_returned mmddyy10.;
format date_left - numeric - date_left mmddyy10.;
That will apply the format when they are numeric, and leave the variables untouched when they are character.
11-26-2012 04:39 PM
Here is another way to approach the problem, using some code stolen from one of Mike Zdeb's papers (see: http://www.nesug.org/Proceedings/nesug11/ds/ds12.pdf )
/*create some test data*/
if ranuni(987) le .2 then
call missing(sex, age);
. = 'MISSING'
other = 'OK'
' ' = 'MISSING'
other = 'OK'
ods output onewayfreqs=tables (keep=table f_: frequency percent);
proc freq data=class;
tables _all_ / missing;
format _numeric_ nm.
ods output close;
if strip(coalescec(of f_) eq "MISSING" ad percent ge 100;
proc sql noprint;
into :renames separated by " ",
:formats separated by " "
data want (drop=_;
set class (rename=(&renames.));