what is the difference between $4. and $char4. formats in sas?
From the online help for $charw. format:
Comparisons
The $CHARw. format is identical to the $w. format.
Hi @ballardw,
I looked up the $charw. format in the documentation and found the below. Would you mind sharing the link are you using?
Thanks & kind regards,
Amir.
@Amir wrote:
Hi @ballardw,
I looked up the $charw. format in the documentation and found the below. Would you mind sharing the link are you using?
Thanks & kind regards,
Amir.
That was from my local 9.4.4 online help. Apparently they changed it sometime after that in the documentation.
I suspect you found this definition.
Which appears to almost be a copy of the definition of the INFORMAT named $CHAR.
The only actual difference between $ and $CHAR as formats is when you include the $CHAR. format specification (without any WIDTH included) in the PUT statement. There the $CHAR format behaves differently from the $ format in two ways:
@Tom wrote:
- You cannot do the same think with the $ format as you cannot use it without a WIDTH included.
You could use the alias $Fw. to omit the width specification. (I wouldn't regard the single dollar sign in a PUT statement as a format specification. It supports list PUT.) But even $F. does not trim trailing blanks (at least in my SAS 9.4M5). I think the behavior of the two formats has not changed since SAS 6 (why should it?) and the newly added statement in the documentation about $w. and trailing blanks seems just wrong.
There is not much difference between the FORMATs of $ and $CHAR.
1 data test ; 2 x=' X'; 3 put '|' x $4. '|' 4 / '|' x $char4. '|' 5 ; 6 run; | X| | X|
There is a difference in how the INFORMATs of $ and $CHAR behave. The $CHAR informat will preserve the leading spaces.
data test ;
input @1 x $char4. @1 y $4. ;
put (x y) (= $quote.);
cards;
4
34
234
1234
;
1 data test ; 2 input @1 x $char4. @1 y $4. ; 3 put (x y) (= $quote.); 4 cards; x=" 4" y="4" x=" 34" y="34" x=" 234" y="234" x="1234" y="1234"
I suspect that the $CHAR format only exists so you could use a FORMAT name that matches the INFORMAT name you used when creating the variable. Just like we frequently see code posted here (and even included in code generated by SAS) that use BEST as if it was an INFORMAT.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.