DATA Step, Macro, Functions and more

Remove blank from "strange" string

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

Remove blank from "strange" string

hi,
i have a problem with some records, this is an example:

data  WORKUSI.T03_TOT_SUM2 (keep= DB_OPER  LENGTHN TIPOPER_ DB_SUB DB_CAU DB_COMP left_ )   ;
set WORKUSI.T03_TOT_SUM ;
LENGTHN = LENGTHN(DB_OPER);
DB_COMP= COMPRESS(DB_OPER)!!TIPOPER_;
DB_TRI= TRIM(DB_OPER)!!TIPOPER_;
DB_CAU = substr(db_oper,1,length(DB_OPER)-10)!!TIPOPER_; 
DB_SUB=  substr(db_oper,1,length(DB_OPER))!!TIPOPER_;
run;

 

example.JPG

DB_OPER is char 60

"CON.CBILL 003D2          "

 

TIPOPER is char 8

"M900000"

 

Lengh of DB_OPER is 25 but the last 10 position are blank.

I need to remove the last 10 blanks from string (DB_OPER): AS IS "CON.CBILL 003D2          "  TO BE  "CON.CBILL 003D2". 

My problem is have different string and the number of blank in the end of the string are varibable: whith function  compress, trim haven't result.

Please help.

thank you very much 

 

 


Accepted Solutions
Solution
4 weeks ago
Super User
Posts: 5,082

Re: Remove blank from "strange" string

First, use the code I suggested to change DB_OPER.  Then use the CATS function.

 

After removing the null characters, there are still trailing blanks to consider.  But CATS will remove those.

View solution in original post


All Replies
Super User
Posts: 5,082

Re: Remove blank from "strange" string

TRIM removes trailing blanks.

 

STRIP removes both leading and trailing blanks.

 

COMPRESS removes all blanks (and can be used to impact other characters, not just blanks).

 

But there is no function that allows the length of a variable to change from one observation to the next.  Each variable has a fixed length.  So if  you want the number of characters to change, with no trailing blanks allowed, you have to do something different when writing out the value of the variable.

Super User
Super User
Posts: 7,401

Re: Remove blank from "strange" string

Hi,

 

Please avoid coding in random case it makes reading the code very hard - look at other code samples here for style guide.  Also avoid using SAS function names as variable names, first it makes reading your code very hard, second it can lead to logic issues (as some functions can be on both sides of the equals).

Now what is your issue exactly?  Strings are made up of boxes to the length of the string - each box can contain a character.  Hence any string with less characters in than the length of the string will have blanks at the end.  This is how they work.

 

If you are after shortening variable lengths, then refer to this post:

https://communities.sas.com/t5/General-SAS-Programming/output-from-iteration/m-p/377253

 

Which is exactly the same question and the same applies here.

Super User
Super User
Posts: 6,500

Re: Remove blank from "strange" string

Your question does not make sense.

I need to remove the last 10 blanks from string (DB_OPER):
AS IS "CON.CBILL 003D2          "  
TO BE "CON.CBILL 003D2"
. 

The variable DB_OPER is defined to have a specific length. When you assign a shorter value to it then SAS will automatically fill out the rest of the string with blanks.

Why does this cause you any trouble?

If want to concatenate values to form a new value then use the proper functions.  So to make 'cowboy' from 'cow' and 'boy' use the CATS() function to remove the blanks.

length x y z $25 ;
x='cow';
y='boy';
z=cats(x,y);
Contributor
Posts: 51

Re: Remove blank from "strange" string

In this case, casts not work:

data  WORKUSI.T03_TOT_SUM2 (keep= DB_OPER  TIPOPER_ z )   ;
set WORKUSI.T03_TOT_SUM ;
z=cats(DB_OPER,TIPOPER_ );
run;

I don't know the reason.

 

 

example2.JPG

 

This is the db SAS:

https://drive.google.com/file/d/0B3qwWJU0WR1cUzJubTNlUWtGM0U/view?usp=sharing

Thank you vary much

M.

Super User
Posts: 5,082

Re: Remove blank from "strange" string

Most likely, your data contains some strange characters that aren't displaying.  They're not blanks, but they look like blanks.  For example, it's possible that the value of DB_OPER contains a carriage return at the end.

 

First, discover what is actually in there by printing in HEX format.  If DB_OPER is defined as $30, test with:

 

put DB_OPER $hex60.;

 

Once the special characters are identified, COMPRESS can remove them.  But first we have to see what they are.

Contributor
Posts: 51

Re: Remove blank from "strange" string

THANK YOU .

 

DB_OPER is $60.

also

DB_HEX = put(DB_OPER,$hex120.);

is it correct ?

 

This is result

DB_HEX = put(DB_OPER,$hex120.);

434F4E2E4342494C4C203030334432000000000000000000002020202020202020202020202020202020202020202020202020202020202020202020

Can I identify special characters?

 

thank's!!

Super User
Posts: 5,082

Re: Remove blank from "strange" string

Yes.  A blank is "20" in hex. You have some null characters here (hex "00");  To get rid of them (essentially replacing them with blanks):

 

db_oper = compress(db_oper, '00'x);

 

Then the rest of your code should work as expected.

Contributor
Posts: 51

Re: Remove blank from "strange" string

Smiley Sad don't work ...

 

data  WORKUSI.T03_TOT_SUM2 (keep= DB_OPER  TIPOPER_ DB_CAU DB_SUB )   ;
set WORKUSI.T03_TOT_SUM ;
DB_CAU = substr(db_oper,1,length(DB_OPER)-10)!!TIPOPER_; 
DB_SUB = compress(db_oper, '00'x)!!TIPOPER_;
run;

example3.JPG

 

 

Do you have any advice?

Thnanks!!!

Solution
4 weeks ago
Super User
Posts: 5,082

Re: Remove blank from "strange" string

First, use the code I suggested to change DB_OPER.  Then use the CATS function.

 

After removing the null characters, there are still trailing blanks to consider.  But CATS will remove those.

Contributor
Posts: 51

Re: Remove blank from "strange" string

Thank you so much!!!
☑ This topic is SOLVED.

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

Discussion stats
  • 10 replies
  • 150 views
  • 1 like
  • 4 in conversation