pick observation or row having longer name in sas

懵懂的女人 提交于 2019-12-25 19:39:50

问题


i have dataset a like this

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

we have to create dataset b from dataset a

id   name          age sex
2  Bonaventure, T  24   F

we have to select data having longer name in all name variable so we have to select the observation having longer name so dataset b contain only one data.


回答1:


You need to find first the maximun length across all records and then compare it against each indiviual record's length. bear in mind that more than one record can meet this criteria. in your example is only one though.

Here is a solution using proc sql

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



回答2:


There are a lot of possibilities you can do it. Here you have example how to obtain a correct result using only one data step.

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;


来源:https://stackoverflow.com/questions/38892740/pick-observation-or-row-having-longer-name-in-sas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!