05-21-2012 01:09 PM
I am using a format for some counties
VALUE $ CNTYSTAT
'01' - '99' = 'KNOWN '
OTHER = 'UNKNOWN'
The problem i am having is
if the county has code like 01A then also it is printing as known.
If the county code is like 01A then it should print it as unknown.
05-21-2012 02:19 PM
If that's what you want to get, then the problem is that you created a character format. As character strings, both of these comparisons are true:
'01' < '01A'
'01A' < '99'
So 01A is in range, and prints as KNOWN. Even a character string like '255' would fall within the KNOWN range.
Fixing the problem is another story. First, create a numeric format instead of a character format. If it's a matter of selecting observations, then select with:
if put( input(county_code, ??3.), cntystat.) = 'KNOWN';
But if it's just a matter of printing, you will need to create a numeric version of COUNTY_CODE, such as:
county_code_num = input(county_code, ??3.);
The ?? will suppress messages about invalid numeric data when you process values such as 01A.
05-21-2012 02:46 PM
Basically like Astounding said, there is no clean-cut solution. You will have to either create another variable like option1 or lay out all of the possibilities using Macro like option2:
b=ifc(lengthn(a)<=2, compress(a,,'kd'), 'ZZZ');
format b $cntystat10.;
value $ CNTYSTAT
%do i=2 %to 99;
%end; &zi = 'KNOWN'
OTHER = 'UNKNOWN'
05-21-2012 10:25 PM
Another way is to make a format on your own .
data have; input a $; cards; 01 0A 02 100 10A ; run; data fmt; retain fmtname 'cntystat' type 'C' ; do i=1 to 99; start=put(i,z2.); label='KNOW '; output; end; hlo='o';start=' ';label='UNKNOW';output; drop i; run; proc format cntlin=fmt; run; data want; set have; format a $cntystat8. ; run;
05-21-2012 11:37 PM
You can do pretty much anything now with formats since you can use functions in 9.3.
proc fcmp outlib=sasuser.test.func32;
function knownzip(ZIP $) $;
return(trim(ifc('01'<=ZIP<='99' and length(ZIP)=2, 'KWOWN', 'UNKNOWN')));
proc format ;
value $ knownzip other=[knownzip()];
A='01 '; put A $knownzip.;
A='01A'; put A $knownzip.;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds