DATA Step, Macro, Functions and more

SAS strip function doesn't work

Accepted Solution Solved
Reply
Senior User
Posts: 1
Accepted Solution

SAS strip function doesn't work

data test;
     char=strip('ALTEPLASE INJECTION 2 MG  ');
run;

 

After running the above program, the two trailing spaces aren't trimmed. Please instruct.


Accepted Solutions
Solution
‎03-29-2018 02:30 PM
Super User
Posts: 10,588

Re: SAS strip function doesn't work


cm292 wrote:

data test;
     char=strip('ALTEPLASE INJECTION 2 MG  ');
run;

 

After running the above program, the two trailing spaces aren't trimmed. Please instruct.


What happens is this:

When SAS encounters such a character assignment, and the length of the receiving variable has not been defined yet, then the length of the source is used, and since character variables are always of fixed length and padded with blanks, the result is identical to the initial string.

Strip in such a case only makes sense when you need to remove leading blanks.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

View solution in original post


All Replies
Super User
Posts: 4,026

Re: SAS strip function doesn't work

The STRIP function is only relevant if you are adding text to the right. Try this:

 

data test;
     char=strip('ALTEPLASE INJECTION 2 MG  ') !! 'Extra text';
run;

If you don't add anything then SAS pads character variables with blanks up to the length the CHAR field is defined as when storing in the dataset TEST.

Solution
‎03-29-2018 02:30 PM
Super User
Posts: 10,588

Re: SAS strip function doesn't work


cm292 wrote:

data test;
     char=strip('ALTEPLASE INJECTION 2 MG  ');
run;

 

After running the above program, the two trailing spaces aren't trimmed. Please instruct.


What happens is this:

When SAS encounters such a character assignment, and the length of the receiving variable has not been defined yet, then the length of the source is used, and since character variables are always of fixed length and padded with blanks, the result is identical to the initial string.

Strip in such a case only makes sense when you need to remove leading blanks.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 167 views
  • 5 likes
  • 3 in conversation