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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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