【问题标题】:pick observation or row having longer name in sas在 sas 中选择具有较长名称的观察或行
【发布时间】:2016-08-11 09:43:26
【问题描述】:

我有一个这样的数据集

id   name          age sex
1  Murray, W       23   M
2  Bonaventure, T  24   F
3  Eberhardt, S    56   M
4  LaMance, K      78   M
5  Underwood, K    23   F

我们必须从数据集 a 创建数据集 b

id   name          age sex
2  Bonaventure, T  24   F

我们必须在所有名称变量中选择名称较长的数据,因此我们必须选择名称较长的观察,因此数据集 b 仅包含一个数据。

【问题讨论】:

  • 这是某种测试/培训练习吗?使用length() 函数,答案相当明显。这甚至不是一个非常特定于 SAS 的问题。

标签: sas


【解决方案1】:

您需要首先找到所有记录的最大长度,然后将其与每个单独记录的长度进行比较。请记住,不止一个记录可以满足此标准。在你的例子中只有一个。

这是使用proc sql的解决方案

proc sql;
create table b as 
select * from a 
    where
        length(name) = (select max(length(name) from a) ;
quit;

【讨论】:

  • 如果您认为回答了您的问题,请接受答案。
【解决方案2】:

您可以通过多种方式做到这一点。 这里有示例如何仅使用一个数据步骤获得正确的结果。

data b;
    retain max obs 0;

    set a end=last;

    if lengthn(name) > max then do;
        max = lengthn(name);
        obs = _N_;
    end;

    if last;
    set a point=obs;
    drop max obs;
run;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2014-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多