PUTC Function

Enables you to specify a character format at run time.

Category: Special

Syntax

PUTC(source, format.<,w> )

Required Arguments

source

specifies a character constant, variable, or expression to which you want to apply the format.

format.

is a character constant, variable, or expression with a value that is the character format you want to apply to source.

Optional Argument

w

is a numeric constant, variable, or expression that specifies a width to apply to the format.

Interaction If you specify a width here, it overrides any width specification in the format.

Details

If the PUTC function returns a value to a variable that has not yet been assigned a length, by default the variable length is determined by the length of the first argument.

Comparisons

The PUTN function enables you to specify a numeric format at run time.
The PUT function is faster than PUTC because PUT lets you specify a format at compile time rather than at run time.

Example

The PROC FORMAT step in this example creates a format, TYPEFMT., that formats the variable values 1, 2, and 3 with the name of one of the three other formats that this step creates. These three formats output responses of "positive," "negative," and "neutral" as different words, depending on the type of question. After PROC FORMAT creates the formats, the DATA step creates a SAS data set from raw data consisting of a number identifying the type of question and a response. After reading a record, the DATA step uses the value of TYPE to create a variable, RESPFMT, that contains the value of the appropriate format for the current type of question. The DATA step also creates another variable, WORD, whose value is the appropriate word for a response. The PUTC function assigns the value of WORD based on the type of question and the appropriate format.
proc format;
   value typefmt 1='$groupx' 
                 2='$groupy'
                 3='$groupz';
   value $groupx 'positive'='agree'
                 'negative'='disagree'
                 'neutral'='notsure ';
   value $groupy 'positive'='accept'
                 'negative'='reject'
                 'neutral'='possible';
   value $groupz 'positive'='pass    '
                 'negative'='fail'
                 'neutral'='retest';
run;
data answers;
   length word $ 8;
   input type response $;
   respfmt = put(type, typefmt.);
   word = putc(response, respfmt);
   datalines;
1 positive
1 negative
1 neutral
2 positive
2 negative
2 neutral
3 positive
3 negative
3 neutral
;
proc print data=answers;
run;
SAS log:
Obs    word        type    response    respfmt                                              1     agree         1     positive    $groupx
 2     disagree      1     negative    $groupx
 3     notsure       1     neutral     $groupx
 4     accept        2     positive    $groupy
 5     reject        2     negative    $groupy
 6     possible      2     neutral     $groupy
 7     pass          3     positive    $groupz
 8     fail          3     negative    $groupz
 9     retest        3     neutral     $groupz
The value of the variable WORD is agree for the first observation. The value of the variable WORD is retest for the last observation.