BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cello23
Quartz | Level 8

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 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

10 REPLIES 10
Astounding
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Tom
Super User Tom
Super User

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);
Cello23
Quartz | Level 8

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.

Astounding
PROC Star

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.

Cello23
Quartz | Level 8

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!!

Astounding
PROC Star

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.

Cello23
Quartz | Level 8

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!!!

Astounding
PROC Star

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.

Cello23
Quartz | Level 8
Thank you so much!!!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 10 replies
  • 4210 views
  • 1 like
  • 4 in conversation