BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

I want to split  field "Customer_Branch"  into two fields: "Customer"  and "branch".

All digits left to minus sign will be in "Customer" field

All digits right  to minus sign will be in "branch" field.

What is the way to do it please?

Data tbl;
input Customer_Branch $30.;
cards;
2345777-783
3444-965
476-814
47474744-981
;
run;

 

6 REPLIES 6
Ronein
Meteorite | Level 14

I found nice solution.

Is there another nice way to do it?

 

Data tbl;
input Customer_Branch $30.;
cards;
2345777-783
3444-965
476-814
47474744-981
;
run;
data tbl2;
set tbl;
Customer=substr(Customer_Branch, 1, find(Customer_Branch, '-')-1);
Branch=substr(Customer_Branch, length(Customer)+2, length(Customer_Branch)-length(Customer)-1);
run;
KachiM
Rhodochrosite | Level 12

@Ronein 

 

Another way is to use 'DLM'. Here is it:

 

Data tbl;
infile datalines dlm = '-';
input Customer Branch ;
datalines;
2345777-783
3444-965
476-814
47474744-981
;
run;
SASKiwi
PROC Star

Another way:

Data tbl;
input Customer_Branch $30.;
cards;
2345777-783
3444-965
476-814
47474744-981
;
run;
data tbl2;
set tbl;
Customer=scan(Customer_Branch, 1, '-');
Branch=scan(Customer_Branch, 2, '-');
run;
sustagens
Pyrite | Level 9
data want;
set tbl;
Customer=substr(Customer_Branch,1,find(Customer_Branch,'-')-1);
Branch=substr(Customer_Branch,find(Customer_Branch,'-')+1);
run;
ed_sas_member
Meteorite | Level 14

Hi @Ronein 

You can use the scan function, which is the easiest way to do this job in your case.

It splits your string in several parts ("words") according to a delimiter that you specify as the third argument (in your case, an hyphen)

You can then retrieve the first, the second, ... word. 

 

data tbl2;
	set tbl;
	Customer = scan(Customer_Branch,1,"-");
	Branch = scan(Customer_Branch,2,"-");
run;

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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