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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 723 views
  • 0 likes
  • 6 in conversation