sas中多列按空格分割字符串

发布于 2025-01-16 21:44:59 字数 823 浏览 4 评论 0原文

嗨,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帥小哥 2025-01-23 21:44:59

代码:

data want;
    length first_name middle_name last_name $50.;

    set have;

    n_names = countw(name);

    if(n_names) = 1 then first_name = name;
        else if(n_names = 2) then do;
            first_name = scan(name, 1);
            last_name  = scan(name, -1);
        end;
            else do;
                first_name  = scan(name, 1);
                last_name   = scan(name, -1);
                middle_name = substr(name, length(first_name)+2, length(name) - (length(first_name) + length(last_name))-2 );
            end;
run;

工作原理

我们知道:

  • 如果有一个单词,它就是名字
  • 如果有两个单词,它就是名字和姓氏
  • 如果有三个或更多单词,它就是名字、姓氏、和中间名

要获得中间名,我们知道:

  • 名字从哪里开始,有多长
  • 姓氏从哪里开始,有多长
  • 整个名字有多长

通过简单地做一些减法,我们可以得到一个子串中间名:

Len  ----------------- 17
     ----5        ---4
     First Middle Last
Pos        7    12

长度字符串是 17。“Middle”从 7 开始,到 12 结束。我们只需从字符串的总长度中减去名字和姓氏的长度就可以得到中间名的长度。我们减去 2 以考虑中间名末尾的空格。

17 - (5 + 4) - 2 = 6

我们的起始位置是 5 + 2(即名字 + 2)以占空间。将其转换为 substr

substr(name, length(first_name)+2, length(name) - (length(first_name) + length(last_name))-2 )

Code:

data want;
    length first_name middle_name last_name $50.;

    set have;

    n_names = countw(name);

    if(n_names) = 1 then first_name = name;
        else if(n_names = 2) then do;
            first_name = scan(name, 1);
            last_name  = scan(name, -1);
        end;
            else do;
                first_name  = scan(name, 1);
                last_name   = scan(name, -1);
                middle_name = substr(name, length(first_name)+2, length(name) - (length(first_name) + length(last_name))-2 );
            end;
run;

How it works

We know:

  • If there's one word, it's a first name
  • If there are two words, it's a first and last name
  • If there are three or more words, it's a first, last, and middle name

To get the middle name, we know:

  • Where the first name starts and how long it is
  • Where the last name starts and how long it is
  • How long the entire name is

By simply doing some subtraction, we can get a substring of the middle name:

Len  ----------------- 17
     ----5        ---4
     First Middle Last
Pos        7    12

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:

substr(name, length(first_name)+2, length(name) - (length(first_name) + length(last_name))-2 )
养猫人 2025-01-23 21:44:59

改编自如何分隔名字、中间名和姓氏

data want;
   set my_data1;
   length first middle middle1 middle2 last $ 40;
   array parts[*] first middle1 middle2 last;

   do i = 1 to countw(name);
      if i = countw(name) and i < dim(parts) then do;
         parts[dim(parts)] = scan(name, i);
      end;
      else do;
         parts[i] = scan(name, i);
      end;
   end;
   
   if middle1 ne "" and middle2 ne "" then middle = catx(" ", middle1, middle2);
   else middle = middle1;
   if first = "" and last ne "" then do;
        first = last;
        last = "";
   end;

   drop name i middle1 middle2;
run;

在此处输入图像描述

Adapted from How to separate first name and middle name and last name

data want;
   set my_data1;
   length first middle middle1 middle2 last $ 40;
   array parts[*] first middle1 middle2 last;

   do i = 1 to countw(name);
      if i = countw(name) and i < dim(parts) then do;
         parts[dim(parts)] = scan(name, i);
      end;
      else do;
         parts[i] = scan(name, i);
      end;
   end;
   
   if middle1 ne "" and middle2 ne "" then middle = catx(" ", middle1, middle2);
   else middle = middle1;
   if first = "" and last ne "" then do;
        first = last;
        last = "";
   end;

   drop name i middle1 middle2;
run;

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文