Search code examples
formatsassas-macro

How Can I Apply Different Formats Based on Data Values


I'm revising a data step that resides in a SAS macro. Currently a single format is used to create a new field for the whole dataset. I'm attempting to revise it to where a different format will be used on different subsets of the dataset. (I have a field that indicates which subset the row belongs to.)

I could do this with a bunch of if statements, but that would need to be changed if the number of categories changes in the future. I think I can achieve what I want by using call symput and resolve but I'm unsure what the syntax for that would be. Can I achieve this without resorting to if statements?


Solution

  • Your question isn't perfectly clear, but if you want to do something like

    newvar = put(oldvar,MYFMT.)
    

    ->

    if x=1 then newvar=put(oldvar,MYFMT.);
    else newvar=put(oldvar,MYFMT2.);
    

    But without all of the if statements, you can use PUTN or PUTC, which allows you to specify the format at runtime.

    newvar = putn(oldvar,fmtvar); *optionally can specify w and d as separate arguments also;