BookmarkSubscribeRSS Feed
SAS_INFO
Quartz | Level 8

Hi All,

 

While using substring function, it ignores the trailing blanks , and i need to retain the trailing blanks of the string,

 

ex.

 

data b;

one= "abc ";

two=substr(one,3);

three=lengthc(one);

four=length(two);

run;

this is the output

one two three  four

abc c    7      1

2 REPLIES 2
PGStats
Opal | Level 21

Replace four=length(two) with four=lengthc(two) and you will see that the trailing blanks are kept in variable two.

 

data b;
one= "abc ";
two=substr(one, 3);
three=lengthc(one);
four=lengthc(two);
five = cat('"', two, '"');
run;

proc print; run;
PG
Kurt_Bremser
Super User

When you put a string into a SAS character variable that is shorter than the defined length of the variable, then it is always padded with blanks (unless you use substr() on the left side).

data test;
length x1 $5 x2 $10 x3 $20;
x1 = 'AAAAA';
x2 = 'XXXXXXXXXX';
x3 = put(x2,$hex20.);
output;
x2 = x1;
x3 = put(x2,$hex20.);
output;
run;

proc print data=test noobs;
run;

Result:

 x1      x2                     x3

AAAAA    XXXXXXXXXX    58585858585858585858
AAAAA    AAAAA         41414141412020202020

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 2003 views
  • 2 likes
  • 3 in conversation