BookmarkSubscribeRSS Feed

Do Custom Formats Exist?

Started ‎07-13-2020 by
Modified ‎07-13-2020 by
Views 1,953

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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