BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

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 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20

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;
Ronein
Onyx | Level 15

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? 

PeterClemmensen
Tourmaline | Level 20

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.

Kurt_Bremser
Super User

@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:

 

count

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:

  • If count is positive, SCAN counts words from left to right in the character string.
  • If count is negative, SCAN counts words from right to left in the character string.

 

Kurt_Bremser
Super User

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.

Satish_Parida
Lapis Lazuli | Level 10
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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1400 views
  • 5 likes
  • 4 in conversation