BookmarkSubscribeRSS Feed

Do Custom Formats Exist?

Started ‎07-13-2020 by
Modified ‎07-13-2020 by
Views 2,031

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;

Version history
Last update:
‎07-13-2020 07:45 AM
Updated by:

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags