SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

splitting a variable into several depending on parenthesis

Accepted Solution Solved
Reply
Super Contributor
Posts: 413
Accepted Solution

splitting a variable into several depending on parenthesis

hi,

suppose I have the following:

name
srjg5483h (djfhtbv  fjfj ) (fhfh 183 f)
fjj8 (f)
fkk   999999

I would like is to create new columns for the text within the parentheses (whenever present) :

nameinfocode
srjg5483h (djfhtbv  fjfj ) (fhfh 183 f)djfhtbv  fjfjfhfh 183 f
fjj8 (f)f
fkk   999999

thank you!


Accepted Solutions
Solution
‎05-05-2015 11:59 AM
Valued Guide
Posts: 858

Re: splitting a variable into several depending on parenthesis

This will give you your desired output:

data have;

infile cards dsd;

length name $50.;

input name $;

cards;

srjg5483h (djfhtbv  fjfj ) (fhfh 183 f)

fjj8 (f)

fkk   999999

;

run;

data want;

set have;

info = scan(name,2,'(,)');

code = scan(name,4,'(,)');

run;

View solution in original post


All Replies
Super User
Posts: 5,257

Re: splitting a variable into several depending on parenthesis

Use scan with ( as delimiter.

Compress ) from the results.

Data never sleeps
Solution
‎05-05-2015 11:59 AM
Valued Guide
Posts: 858

Re: splitting a variable into several depending on parenthesis

This will give you your desired output:

data have;

infile cards dsd;

length name $50.;

input name $;

cards;

srjg5483h (djfhtbv  fjfj ) (fhfh 183 f)

fjj8 (f)

fkk   999999

;

run;

data want;

set have;

info = scan(name,2,'(,)');

code = scan(name,4,'(,)');

run;

Super User
Posts: 9,682

Re: splitting a variable into several depending on parenthesis



data have;
infile cards dsd;
length name $50.;
input name $;
cards;
srjg5483h (djfhtbv  fjfj ) (fhfh 183 f)
fjj8 (f)
fkk   999999
;
run;

proc sql;
select max(countc(name,'()'))/2 into : n separated by ' ' from have;
quit;
data want;
 set have;
 array x{*} $ 100 v1-v&n;
 do i=1 to &n ;
  x{i}=scan(name,i*2,'()','m');
 end;
 drop i;
 run;

Xia Keshan

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 362 views
  • 4 likes
  • 4 in conversation