如何在 SAS 中连接一个观察中的每条记录

发布于 2024-10-26 01:35:15 字数 684 浏览 7 评论 0原文

我想知道是否可以将一个观察中的每条记录与 SAS 代码连接起来。 例如,

这是原始数据集

1st_name 2nd_name 3rd_name .....last_name

   abc   def      ghi .....     xyz

现在我想添加一个变量,用于连接从 1st_name 到 last_name 的所有值 - 如果可能的话,用特定的分隔符分隔。

预期结果

1st_name 2nd_name 3rd_name .....last_name   all_name

   abc   def      ghi .....     xyz     abcdefg...xyz 

当然有一种方法

data name;
  set name;
  length all_name $ 30;
  all_name=1st_name||2nd_name....||last_name;
run;

但是,如果有数百个变量,事情会变得很糟糕。所以问题是如何自动完成,而无需指定变量名称、数字等。

期待 SAS 专家的解答:)

I wonder whether it's possible to concatenate each record within one observation with SAS code.
E.g.

Here is the original data set

1st_name 2nd_name 3rd_name .....last_name

   abc   def      ghi .....     xyz

Now I want to add a variable that concatenates all values from 1st_name to last_name--separated by a specific separator, if possible.

Expected result

1st_name 2nd_name 3rd_name .....last_name   all_name

   abc   def      ghi .....     xyz     abcdefg...xyz 

Of course there is one way

data name;
  set name;
  length all_name $ 30;
  all_name=1st_name||2nd_name....||last_name;
run;

However, things will get terrible if there are hundreds of variables. So the question has been how to do it automatically, without having to specify variable names, numbers etc.

Looking forward to answers from SAS experts:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

述情 2024-11-02 01:35:15

下面的代码应该可以工作。您需要根据自己的情况进行调整。它的作用是创建一个所有字符变量的数组。如果名称包含值名称,则会将其连接起来。 catx 函数修剪值并使用第一个参数作为分隔符。请注意,SAS 字段的最大大小为 32767 个字符,因此连接数百个变量可能会导致错误。

data concatnames (drop=i);
 * maximum field length, will contain concatenated names;
 attrib all length=$32767.;
 * read source dataset;
 set names;
 * create array with with character fields;
 array char_array {*} _character_;
 * loop through array;
 do i = 1 to dim(char_array);
  * if fieldname contains name, then add to all with a dash as seperator;
  if (index(vname(char_array{i}),"name") ne 0) then all = catx("-",all,char_array{i});
 end;
run;

The code below should work. You'll need to tweak it to your own situation. What it does is creating an array of all character variables. If the name contains the value name it will be concatenated. The catx functions trims the values and uses the first parameter as seperator. Just be aware that the maximum size of a SAS field is 32767 characters, so concatenating hundreds of variables might result in an error.

data concatnames (drop=i);
 * maximum field length, will contain concatenated names;
 attrib all length=$32767.;
 * read source dataset;
 set names;
 * create array with with character fields;
 array char_array {*} _character_;
 * loop through array;
 do i = 1 to dim(char_array);
  * if fieldname contains name, then add to all with a dash as seperator;
  if (index(vname(char_array{i}),"name") ne 0) then all = catx("-",all,char_array{i});
 end;
run;
寒江雪… 2024-11-02 01:35:15

如果要连接的变量按顺序排列,您可以使用带有变量列表(双破折号)的“of”语法来简化代码。

data name;
  length all_name $32767.;
  set name;
  allname=cats(of first_name--last_name);
run;

If the variables to concatenate are in order, your can use the "of" syntax with a variable list (the double dash) to simplify the code.

data name;
  length all_name $32767.;
  set name;
  allname=cats(of first_name--last_name);
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文