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;
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
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.
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.
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.
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);
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.
This is the db SAS:
https://drive.google.com/file/d/0B3qwWJU0WR1cUzJubTNlUWtGM0U/view?usp=sharing
Thank you vary much
M.
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.
THANK YOU Astounding .
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!!
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.
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;
Do you have any advice?
Thnanks!!!
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.