Do custom formats really exist?
While Proc FORMAT has many benefits to group and transform data values and has been used by all SAS programmers from the very beginning, there may be unique cases to group by keywords in character variables. This non-standard method of grouping can not easily be defined by unique values or minimum or maximum values. This can be done using INDEXW() function within a DATA Step, but this requires creating a new grouping variable. One of the unqiue features of applying formats is keeping the original value but displaying it based on user requirements.
By using the power of PROC FCMP to create a custom function and PROC FORMAT option to apply the custom function, we are able to apply more robust techiques to search and group based on keywords without having to create a new variable. This gives PROC FORMAT metadata features. Thanks to Vince DelGobbo for the solution below to identify and group drug names for Aspirin keyword. From the drug values below, as long as is 'aspirin' is within the text, it will be grouped to 'Aspirin'.
options cmplib=(work.functions);
proc fcmp outlib=work.functions.formats;
function aspirin_format(drug_name $) $;
length drug_type $11;
if (indexw(lowcase(drug_name), 'aspirin') gt 0)
then drug_type = 'Aspirin';
else drug_type = 'Not Aspirin';
return(drug_type);
endsub;
run; quit;
data work.drug;
length drug1 drug2 $45;
infile cards truncover;
input drug1 $char45.;
drug2 = drug1;
cards;
Aspirin
aspirin 100mg
aspirin 50mg
aspirin 10% HCl
50 mg Aspirin
ibuprofen
;
run;
proc format;
value $aspirin other=[aspirin_format()];
run; quit;
proc print data=work.drug label noobs;
var drug1 drug2;
format drug2 $aspirin.;
label drug1 = 'Drug'
drug2 = 'Type';
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.