BookmarkSubscribeRSS Feed

Do Custom Formats Exist?

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

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:

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags