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

Hi ,

 

How to remove the leading and traling spacce inside brackets.

 

My data is below.

 

data have;

input_var $200;

cards;

( 12.5)

(   1.5)

(111.9)

(  2.8)

( 20.0)

;

run;

Thank you,

Rajasekhar.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
  input var $char80.;
cards;
234  ( 23.5)
  0
 12  (   34)
 30  ( 12.5)
 34  ( 0.5 )
;


data want;
  set have;
  new_var=prxchange('s/\(\s*([\d\.,]+)?\s*\)/(\1)/',-1,var);
run;

View solution in original post

7 REPLIES 7
Shmuel
Garnet | Level 18
new_var = compress(var);
raja777pharma
Fluorite | Level 6

Hi ,

Thank you for response.

 

I am wrongly post the data , i need to remove the space in side the bracket only.

 

data have;

infile datalines truncover;

input_var $200;

234  (  23.5)

0

12   (   34)

30 ( 12.5)

34  ( 0.5 )

;

run;

 

Reeza
Super User
Show the code used to create that field instead and we can show you how to modify it.
ballardw
Super User

@raja777pharma wrote:

Hi ,

Thank you for response.

 

I am wrongly post the data , i need to remove the space in side the bracket only.

 

data have;

infile datalines truncover;

input_var $200;

234  (  23.5)

0

12   (   34)

30 ( 12.5)

34  ( 0.5 )

;

run;

 


That data step still does not run. Incorrect statement: input _var  NOT input_var

The Informat should end a dot: $200.

 

Tom
Super User Tom
Super User

If the strings only have the one parenthetical value then I would just rebuild it.

data have;
  input var $char80.;
cards;
234  ( 23.5)
  0
 12  (   34)
 30  ( 12.5)
 34  ( 0.5 )
;


data want;
  set have;
  new_var=var;
  if index(var,'(') then 
    new_var = substrn(var,1,index(var,'('))
            ||compress(substrn(var,index(var,'(')+1))
  ;
  put (var new_var) (:$quote./) /;
run;

Results:

"234  ( 23.5)"
"234  (23.5)"

"  0"
"  0"

" 12  (   34)"
" 12  (34)"

" 30  ( 12.5)"
" 30  (12.5)"

" 34  ( 0.5 )"
" 34  (0.5)"

PS: Your British is showing.  The characters used to include parentheticals are called parentheses.  Brackets look like this: [ ].

ballardw
Super User

You should show what you expect the result to be so can have a chance of seeing if our suggestions do as desired.

Also, please run your data step and make sure it creates the desired data. Yours has issues as posted.

 

This is my attempt.

data have;
infile datalines truncover;
input _var $200. ;
_var = compress(_var,' ');
cards;
( 12.5)
(   1.5)
(111.9)
(  2.8)
( 20.0)
;

The Truncover is because you are requesting reading so many characters with the $200. informat (incorrectly specified in your code) that SAS will attempt to read from following lines and only gets a couple of records, AFTER fixing the informat and incorrect INPUT statement.

 

Compress function removes characters.

Ksharp
Super User
data have;
  input var $char80.;
cards;
234  ( 23.5)
  0
 12  (   34)
 30  ( 12.5)
 34  ( 0.5 )
;


data want;
  set have;
  new_var=prxchange('s/\(\s*([\d\.,]+)?\s*\)/(\1)/',-1,var);
run;

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
  • 7 replies
  • 608 views
  • 0 likes
  • 6 in conversation