楼主: gaotao0727
3864 6

多个变量取不同值的个数 [推广有奖]

  • 1关注
  • 10粉丝

已卖:67份资源

副教授

79%

还不是VIP/贵宾

-

威望
0
论坛币
545 个
通用积分
11.1290
学术水平
18 点
热心指数
18 点
信用等级
12 点
经验
27715 点
帖子
873
精华
0
在线时间
855 小时
注册时间
2011-8-8
最后登录
2025-9-11

楼主
gaotao0727 发表于 2016-3-3 00:00:50 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
有如下数据集:
  1. data test;
  2.   input x1 $ x2 $ x3 $ x4 $ x5 $ x6 $;
  3.   cards;
  4. c1 c3 c5 c3 c1 c2
  5. c2 c8 c9 c2 c9 c8
  6. ;
  7. run;
复制代码
最后生成一个num变量,是x1~x6这6个变量中取不同值的个数,比如第一条记录的6个变量值去重后是c1,c2,c3,c5,因此num=4;

谢谢高手指点!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:多个变量 cards Input test Data 记录

衣带渐宽终不悔,为伊消得人憔悴~~

沙发
yingzi2003 发表于 2016-3-3 03:15:50
data test;
  input x1 $ x2 $ x3 $ x4 $ x5 $ x6 $ ;
  cards;
c1 c3 c5 c3 c1 c2
c2 c8 c9 c2 c9 c8
;
run;

data test ;
  set test end=eof ;
  if eof then call symput("TotalRecords", _n_) ;
run;
%put &totalRecords ;

proc transpose data = test out=one ;
  var x1 - x6 ;
run;

%macro getcount ;
proc sql ;
  create table two as
  select %do i=1 %to &totalRecords ;
           col&i,
         %end ;
         _NAME_
  from one
  union
  select %do i=1 %to &totalRecords ;
           strip(put(count(distinct col&i.), best8.)) as col&i ,
                 %end ;
                 'Num' as _NAME_
  from one ;
quit ;
%mend getcount ;

%getcount

proc transpose data=two out=temp(drop=_name_) ;
  id _NAME_ ;
  var COL: ;
run;

data temp (drop=num);
  set temp ;
  num_count=input(num,best8.) ;
run;




已有 1 人评分论坛币 学术水平 热心指数 收起 理由
admin_kefu + 35 + 1 + 2 热心帮助其他会员

总评分: 论坛币 + 35  学术水平 + 1  热心指数 + 2   查看全部评分

藤椅
gaotao0727 发表于 2016-3-3 13:24:45
yingzi2003 发表于 2016-3-3 03:15
data test;
  input x1 $ x2 $ x3 $ x4 $ x5 $ x6 $ ;
  cards;
思路不错,必须赞一个~~

板凳
山久丰 发表于 2016-3-3 18:03:52
data test;
  input x1 $3. x2 $3. x3  $3. x4  $3. x5 $3.  x6 $3.;
  cards;
c1 c3 c5 c3 c1 c2
c2 c8 c9 c2 c9 c8
;
run;



data test_one;
        set test;
        array x(6) x1-x6;
        array a(6) $ a1-a6;
        a1=x1;
        count=0;
        num_count=0;
        do i=2 to 6;
                do j=1 to i-1;
                if x(i) eq a(j) then count=1;
                end;
                if count eq 0 then a(j)=x(i);
                count=0;
        end;

        do j=1 to 6;
                if a(j) ne '' then num_count+1;
        end;

        drop a1-a6 count i j;
run;

报纸
yongyitian 发表于 2016-3-3 20:32:29
  1. data test;
  2.       input x1 $ x2 $ x3 $ x4 $ x5 $ x6 $;
  3.       cards;
  4.     c1 c3 c5 c3 c1 c2
  5.     c2 c8 c9 c2 c9 c8
  6.     ;
  7.     run;
  8. %let max_num=10;
  9. data want;
  10.     set test;
  11.         array y (&max_num);
  12.         array x (6);
  13.         do i = 1 to 6;
  14.         y[ compress(x[i], 'c') ]=compress(x[i], 'c');
  15.         end;
  16.         num = dim(y) - nmiss(of y[*]);
  17.         drop i y:;
  18. run;
复制代码
已有 1 人评分论坛币 收起 理由
admin_kefu + 25 热心帮助其他会员

总评分: 论坛币 + 25   查看全部评分

地板
yongyitian 发表于 2016-3-4 08:54:18
  1.    data test;
  2.       input x1 $ x2 $ x3 $ x4 $ x5 $ x6 $;
  3.       cards;
  4.     c1 c3 c5 c3 c1 c2
  5.     c2 c8 c9 c2 c9 c8
  6.     ;
  7.     run;
  8. %let max_num=10;
  9. data want;
  10.     set test;
  11.         array y (&max_num);
  12.         array x (6);
  13.         do i = 1 to 6;
  14.         y[compress(x[i], 'c')]=compress(x[i], 'c');
  15.         end;
  16.         num = dim(y) - nmiss(of y[*]);
  17.         drop i y:;
  18. run;
复制代码

7
yongyitian 发表于 2016-3-4 09:01:30
  1.    data test;
  2.       input x1 $ x2 $ x3 $ x4 $ x5 $ x6 $;
  3.       cards;
  4.     c1 c3 c5 c3 c1 c2
  5.     c2 c8 c9 c2 c9 c8
  6.     ;
  7.     run;
  8. %let max_num=10;
  9. data want;
  10.     set test;
  11.         array y (&max_num);
  12.         array x (6);
  13.         do i = 1 to 6;
  14.         y[compress(x[i], 'c')]=compress(x[i], 'c');
  15.         end;
  16.         num = dim(y) - nmiss(of y[*]);
  17.         drop i y:;
  18. run;
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-21 17:57