Hello
I have 2 questions please:
1- I create a data set and I get only 4 rows but in the data set there are 8 rows
2-I want to create new 2 fields:
branch -take the digits in left side of / symbol in Branch_id field
ID-Take the digits in right side of / symbol in Branch_id field
Data example;
input Branch_id $200.;
cards;
727 - 6920351
616 - 4312710
851 - 37366723
641 - 2053927
921 - 8592059
891 - 6794396
932 - 44851042
22-99999999
;
run;
1)
Data example;
input Branch_id $ 1-15;
cards;
727 - 6920351
616 - 4312710
851 - 37366723
641 - 2053927
921 - 8592059
891 - 6794396
932 - 44851042
22-99999999
;
run;
2)
data want;
set example;
branch = scan(Branch_id, 1, '-');
id = scan(Branch_id, 2, '-');
run;
1)
Data example;
input Branch_id $ 1-15;
cards;
727 - 6920351
616 - 4312710
851 - 37366723
641 - 2053927
921 - 8592059
891 - 6794396
932 - 44851042
22-99999999
;
run;
2)
data want;
set example;
branch = scan(Branch_id, 1, '-');
id = scan(Branch_id, 2, '-');
run;
It is great.
Now i want to understand please:
1-Why this statements didn't work
input Branch_id $200.;
2-
branch = scan(Branch_id, 1, '-');
id = scan(Branch_id, 2, '-');
What is the meaning of numbers "1" and "2" in scan function?
1) See @Kurt_Bremsers answer below.
2) I treat - as a delimiter in the Scan Function and take the first (1) word and assign it to branch and the second word (2) and assign it to id.
@Ronein wrote:
It is great.
Now i want to understand please:
1-Why this statements didn't work
input Branch_id $200.;
2-
branch = scan(Branch_id, 1, '-'); id = scan(Branch_id, 2, '-');
What is the meaning of numbers "1" and "2" in scan function?
Regarding your question 2:
Maxim 1: Read the Documentation:
is a nonzero numeric constant, variable, or expression that has an integer value. The integer value specifies the number of the word in the character string that you want SCAN to select. For example, a value of 1 indicates the first word, a value of 2 indicates the second word, and so on. The following rules apply:
Datalines are by definition padded to 80 characters; reading with a $200. format causes a skip to the next input line (you can see this in the log - Maxim 2!).
Use the truncover option to read with an overlong format:
Data example;
infile cards truncover;
input Branch_id $200.;
cards;
727 - 6920351
616 - 4312710
851 - 37366723
641 - 2053927
921 - 8592059
891 - 6794396
932 - 44851042
22-99999999
;
For breaking up a string with a delimiter, use the scan() function.
Data example;
infile cards dlm='-';
input Branch id;
cards;
727 - 6920351
616 - 4312710
851 - 37366723
641 - 2053927
921 - 8592059
891 - 6794396
932 - 44851042
22-99999999
;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.