sas中多列按空格分割字符串
嗨,我对 sas 有一个疑问 sas中如何将字符串拆分为多列? 这里,在第一个空格值之前考虑为名字,在值之后的最后一个空格考虑为姓氏,在第一个和最后一个空格值之间考虑为中间名。
data my_data1;
input name $500.;
datalines;
Andy Lincoln Bernard ravni
Barry Michael
Chad Simpson Smith
Eric
Frank Giovanni Goodwill
;
run;
proc print data=my_data1;
基于数据预期如下所示:
Fname | Middlename | lname
Andy | Lincoln Bernard |ravni
Barry | |Michael
Chad | Simpson |Smith
Eric | |
Frank|Giovanni |Goodwill
我尝试了如下
data my_data2;
set my_data1;
Fname=scan(name, 1, ' ');
Middlename=scan(name, 2, ' ');
Lname=scan(name, -1, ' ');
run;
proc print data=my_data2;
所示的逻辑,但没有给出预期的输出。
你能告诉我如何编写代码在 sas 中完成此任务吗
Hi I have one doubt in sas
How to split string into multiple columns in sas?
Here before first space value consider as firstname and last space after values consider as lastname and between first and lastspace values consider as middle name.
data my_data1;
input name $500.;
datalines;
Andy Lincoln Bernard ravni
Barry Michael
Chad Simpson Smith
Eric
Frank Giovanni Goodwill
;
run;
proc print data=my_data1;
based on data expecte out like below :
Fname | Middlename | lname
Andy | Lincoln Bernard |ravni
Barry | |Michael
Chad | Simpson |Smith
Eric | |
Frank|Giovanni |Goodwill
I tried like below
data my_data2;
set my_data1;
Fname=scan(name, 1, ' ');
Middlename=scan(name, 2, ' ');
Lname=scan(name, -1, ' ');
run;
proc print data=my_data2;
above logic not give expected out put.
can you please tell me how to write code achive this task in sas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
代码:
工作原理
我们知道:
要获得中间名,我们知道:
通过简单地做一些减法,我们可以得到一个子串中间名:
长度字符串是 17。“Middle”从 7 开始,到 12 结束。我们只需从字符串的总长度中减去名字和姓氏的长度就可以得到中间名的长度。我们减去 2 以考虑中间名末尾的空格。
17 - (5 + 4) - 2 = 6
我们的起始位置是 5 + 2(即名字 + 2)以占空间。将其转换为
substr
:Code:
How it works
We know:
To get the middle name, we know:
By simply doing some subtraction, we can get a substring of the middle name:
The length of the string is 17. "Middle" starts at 7 and ends at 12. We can get the length of the middle name by simply substracting the lengths of the first and last names from the total length of the string. We subtract 2 to account for the space at the end of the middle name.
17 - (5 + 4) - 2 = 6
Our start position is 5 + 2 (i.e. the first name + 2) to account for the space. Translating this to
substr
:改编自如何分隔名字、中间名和姓氏
Adapted from How to separate first name and middle name and last name