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;
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;
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;
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;
data want;
set tbl;
Customer=substr(Customer_Branch,1,find(Customer_Branch,'-')-1);
Branch=substr(Customer_Branch,find(Customer_Branch,'-')+1);
run;
This is the classic case for the scan() function, see @SASKiwi
KISS (Maxim 29)
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;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.