BookmarkSubscribeRSS Feed
Sathya3
Obsidian | Level 7

what is the difference between $4. and $char4. formats in sas?

6 REPLIES 6
ballardw
Super User

From the online help for $charw. format:

Comparisons

  • The $CHARw. format is identical to the $w. format.

 

Amir
PROC Star

Hi @ballardw,

 

I looked up the $charw. format in the documentation and found the below. Would you mind sharing the link are you using?

 

Amir_0-1697729791413.png

 

 

Thanks & kind regards,

Amir.

ballardw
Super User

@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?

 

Amir_0-1697729791413.png

 

 

Thanks & kind regards,

Amir.


That was from my local 9.4.4 online help. Apparently they changed it sometime after that in the documentation.

 

 

Tom
Super User Tom
Super User

I suspect you found this definition.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n1oily9h2mwp5cn1dtb5qu8tcj7u.ht...

 

Which appears to almost be a copy of the definition of the INFORMAT named $CHAR.

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/n06dgsx9u83oren1answzdrj9e3h.ht...

 

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:

 

  • You cannot do the same think with the $ format as you cannot use it without a WIDTH included.
  • The leading and trailing spaces are written.

 

FreelanceReinh
Jade | Level 19

@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.

Tom
Super User Tom
Super User

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.

 

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 1326 views
  • 2 likes
  • 5 in conversation