BookmarkSubscribeRSS Feed
pavank
Quartz | Level 8

Hi Guys 

Here count function modifier 't'  or 'T' doesn't trim trailing blanks where i was wrong please fix error

data count;
name="Tokyo Olympics 2020          ";
x=count(name,'a');
x1=count(name,'a','i');
x2=count(name,'2','T');
xlength=length(x);
x1ength=length(x1);
x2length=length(x2);

run;
4 REPLIES 4
Kurt_Bremser
Super User

COUNT returns a number; using LENGTH on a number will cause that number to be converted to character using the BEST. format (IIRC, BEST12.).

pavank
Quartz | Level 8
data count;
name="Tokyo Olympics 2020          ";
x=count(name,'a');
x1=count(name,'a','i');
x2=count(name,'a','T');
xlength=length(x);
x1ength=length(x1);
x2length=length(x2);


run;

x2length    result length is 12 even if I use letter 'a' 

andreas_lds
Jade | Level 19

I will just repeat what @Kurt_Bremser already said:

  1. count returns a number
  2. using the function length on a number is not possible, so sas converts the number into a string (see the log: "Numeric values have been converted to character values")
  3. sas uses the format best12 to convert the number => the string will always have the length of 12.
Kurt_Bremser
Super User

@pavank wrote:
data count;
name="Tokyo Olympics 2020          ";
x=count(name,'a');
x1=count(name,'a','i');
x2=count(name,'a','T');
xlength=length(x);
x1ength=length(x1);
x2length=length(x2);


run;

x2length    result length is 12 even if I use letter 'a' 


That's because the BEST12. format results in a string with the number right-adjusted, and LENGTH counts all characters up to the last non-blank character, including all leading blanks.

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
  • 4 replies
  • 1247 views
  • 0 likes
  • 3 in conversation