JaneA
JaneA

Reputation: 87

SAS: When using user defined formats, if there's not a match, "default value" is the unformatted input variable?

In SAS EG, I have a user defined format

value $MDC
'001' = '77'
'002' = '77 
...
'762' = '14' 
etc.

My data set has DRG_code string variables with values like '001' and '140'.

I was trying to create a new variable, with the below code.

MDC = put(DRG_code, $MDC.)

Only there are more values for the variable DRG_code in my data set, then specified in the user defined format file, $MDC.

For example, when the data set DRG_Code equals '140' this value does not exist in the user defined format, and for some reason the put statement is returning MDC = '14' (which should only be its value with the DRUG code is '762').

Is there a way to make sure my put statement only returns a value from the user defined format when a corresponding value is present?

Grateful for feedback.

Lori

I've tried using formatting like "length" to have my put statement return 3, which I thought would result in "140" instead of "14" and that didn't work.

value $MDC
'001' = '77'
'002' = '77 
...
'762' = '14' 

MDC = put(DRG_code, $MDC.)

Upvotes: 0

Views: 1223

Answers (4)

Tom
Tom

Reputation: 51601

Formats have a DEFAULT width. If you do not specify a width when using the format then SAS will use the default width. When making a user defined format PROC FORMAT will set the default width to the maximum width of the formatted values. In your example the default width is being set to 2.

You can override that when you use the format.

MDC = put(DRG_code, $MDC3.)

Or you could define the default when you define the format.

value $MDC (default=3)
  '001' = '77'
  '002' = '77'
  ...
  '762' = '14' 
;

You can also set a default value for the unmatched values using the other keyword.

value $MDC (default=3)
  '001' = '77'
  '002' = '77'
  ...
  '762' = '14' 
  other = 'UNK'
;

You can even nest a call to another format for the unmatched values (or any target format). In which case you do not need to specify the default width since the width on the nested format will be used when defining the default width.

value $MDC 
  '001' = '77'
  '002' = '77'
  ...
  '762' = '14' 
  other = [$3.]
;

Upvotes: 2

mastropi
mastropi

Reputation: 1414

Here I propose a slight modification to user667489's solution so that:

  • you don't need to specify the length of the format every time you use it (using the default option of the value statement when defining the format)
  • the resulting formatted value doesn't have trailing blanks (using the trim() function on the output resulting from applying the format)

i.e.

proc format;
    value $MDC(default=3)
        '001' = '77'
        '002' = '77'
        '762' = '14'
    ;
run;

data _null_;
    do var = '001', '140', '762';
        var_formatted = quote(trim(put(var, $MDC.)));
        put var= var_formatted=;
    end;
run;

which gives the following output:

var=001 var_formatted="77"
var=140 var_formatted="140"
var=762 var_formatted="14"

Upvotes: 0

user667489
user667489

Reputation: 9569

You can also fix this by specifying a length to use when applying the format, e.g.

proc format;
  value $MDC
  '001' = '77'
  '762' = '14'
  ;
run;

data _null_;
  do var = '001','140','762';
    var_formatted = quote(put(var,$MDC3.));
    put var= var_formatted=;
  end;
run;

Output:

var=001 var_formatted="77 "
var=140 var_formatted="140"
var=762 var_formatted="14 "

N.B. both this solution and Richard's will result in trailing whitespace being added to formatted values, as you can see from the quotes.

Upvotes: 0

Richard
Richard

Reputation: 27508

I presume all the value mappings were $2 because that is what is used for an 'unfound' source value. In order to ensure the length of 'unfound' values, make sure one of the formatted values has trailing spaces filling out to length of longest unfound value.

value $MDC
'001' = '77     ' /* 7 characters, presuming no DRG_code exceeds 7 characters */
'002' = '77'
'762  = '14'

Upvotes: 0

Related Questions