HI all,
I am working with a data sets which has names like
Nichols, KregFI |
its in the form "lastname,(space)firstnamemiddlename"
I accept my o/p to be like this :
firstname=kerg
middlename=Fl
lastname=Nichols
the code for that:
data b;
str='Nichols, KregFI'; /*** String **/
a=index(str,",")+2; /** for finding the position of first Name **/
first_name=substr(scan(str,2,","),1,a-1); /** for first name only**/
run;
But ,I am getting o/p :
s.no str a first_name
1 | Nichols, KregFI | 10 | KregFI |
please correct and assist me for that.
thanks,
Very similar to what Astounding just said, but I'd add the space character into the scan delimiter listing as well just to confirm that the space doesn't offset our numbers. Here's my thought process to help you understand the code in another way:
Last_Name = SCAN(name, 1, ",");
First_Middle = SCAN(name, 2, ", ");
First_Name = SUBSTR(First_Middle, 1, LENGTH(First_Middle)-2);
Middle_Init = SUBSTR(First_Middle, LENGTH(First_Middle)-1, 2);
Last name is easiest to grab; you've already got that. Then you can take the cluster of First and Middle initials together. First name is the substring of everything EXCEPT those last two characters, middle initials are ONLY the last two characters. (These assignments should also be inside a data step, to be clear)
It's probably easier to start this way:
last_name = scan(str, 1, ',');
first_name = scan(str, 2, ',');
At this point, FIRST_NAME still has the initials at the end. But you should be able to find a way to split them off into a separate variable, such as:
middle_name = substr(first_name, length(first_name)-1);
first_name = substr(first_name, 1, length(first_name)-2);
Very similar to what Astounding just said, but I'd add the space character into the scan delimiter listing as well just to confirm that the space doesn't offset our numbers. Here's my thought process to help you understand the code in another way:
Last_Name = SCAN(name, 1, ",");
First_Middle = SCAN(name, 2, ", ");
First_Name = SUBSTR(First_Middle, 1, LENGTH(First_Middle)-2);
Middle_Init = SUBSTR(First_Middle, LENGTH(First_Middle)-1, 2);
Last name is easiest to grab; you've already got that. Then you can take the cluster of First and Middle initials together. First name is the substring of everything EXCEPT those last two characters, middle initials are ONLY the last two characters. (These assignments should also be inside a data step, to be clear)
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.