楼主: dxystata
2165 9

不能用proc freq过程 [推广有奖]

版主

大师

36%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183151 个
通用积分
15259.3409
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
296347 点
帖子
5519
精华
1
在线时间
13589 小时
注册时间
2006-6-21
最后登录
2024-9-19

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

100论坛币
  1. data a;
  2. input x y;
  3. cards;
  4. 1 2
  5. 1 1
  6. 1 1
  7. 2 2
  8. 2 2
  9. ;
  10. run;
复制代码


希望得到的结果
var cat  n
x     1   3
       2   2
y     1   2
       2  3
注意不能用proc freq过程,macro程序中可以用data步和sql过程。

关键词:freq REQ Fre ROC 不能用 程序
沙发
tangliang0905 发表于 2013-12-3 05:45:34 |只看作者 |坛友微信交流群
抛砖引玉

proc sql;
create table a1 as
select
       distinct A.x as cat,
       count(A.x) as n,
           'x' as id
from a as A
group by a.x

union all

select
       distinct a.y as cat,
           count(a.y) as n,
           'y' as id
from a as A
group by a.y
;
quit;

使用道具

藤椅
wwang111 发表于 2013-12-3 09:07:38 |只看作者 |坛友微信交流群
%macro cnt;

proc sql noprint;
select name into: varlist separated by " " from dictionary.columns
where libname='WORK' and memname='A';
quit;

%let i=1;
%do %until(%scan(&varlist,&i)=%str( ));
proc sql;
create table a&i as
select "%scan(&varlist,&i)" as var, %scan(&varlist,&i) as cat,count(%scan(&varlist,&i)) as n
from a
group by 1,2;
quit;
%let i=%eval(&i+1);
%end;

%let cnt=%sysfunc(count(&varlist,%str( )));
data combine;
set
%do i=1 %to %eval(&cnt+1);
a&i
%end;
;
run;
%mend;
%cnt

data wanted;
set combine;
by notsorted var;
if not first.var then var='';
run;
只有一个罗纳尔多

使用道具

板凳
hopewell 发表于 2013-12-3 12:44:48 |只看作者 |坛友微信交流群
  1. data _null_;
  2.     if _n_=1 then do;
  3.         declare hash h(hashexp:4);
  4.         rc=h.definekey('var','cat');
  5.         rc=h.definedata('var','cat','n');
  6.         rc=h.definedone();
  7.     end;
  8.     set a end=last;
  9.     array numarr x y;
  10.     do over numarr;
  11.         var=vname(numarr); cat=numarr;
  12.         n=.; rc=h.find(); n+1;
  13.         if rc then rc=h.add();
  14.         else rc=h.replace();
  15.     end;
  16.     if last then h.output(dataset:'b');
  17. run;
复制代码
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Tigflanker + 1 + 1 + 1 热心帮助其他会员

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

观钓颇逾垂钓趣 种花何问看花谁

使用道具

报纸
qqyy402 发表于 2013-12-3 17:32:27 |只看作者 |坛友微信交流群
我就想到简单的分开用proc univariate来做,再把结果set到一起

proc univariate data=a;
class x;
var x ;
output out=b
n=n;
run;

proc univariate data=a;
class y;
var y ;
output out=c
n=n;
run;

data b;
length var $1.;
set b;
var='x';
rename x=cat;
run;

data c;
length var $1.;
set c;
var='y';
rename y=cat;
run;

data d;
set b c;
run;
Life is like a Markov chain.
You never know where you gonna go.

使用道具

地板
farmman60 发表于 2013-12-4 08:33:04 |只看作者 |坛友微信交流群

data b(keep=group cat);
set a;
array newvar _numeric_;
do over newvar;
  group=vname(newvar);
  cat=newvar;
  output;
end;
run;

proc tabulate data=b;
class group cat;
table group='var'*cat,n*f=2.;
run;

使用道具

7
Tigflanker 发表于 2013-12-4 10:10:37 |只看作者 |坛友微信交流群
hopewell 发表于 2013-12-3 12:44
程序员的惯用做法。。
Bye SAS.
若有缘,能重聚。

使用道具

8
邓贵大 发表于 2013-12-4 22:09:05 |只看作者 |坛友微信交流群
楼主何其多金也!
Be still, my soul: the hour is hastening on
When we shall be forever with the Lord.
When disappointment, grief and fear are gone,
Sorrow forgot, love's purest joys restored.

使用道具

9
bobguy 发表于 2013-12-5 11:55:33 |只看作者 |坛友微信交流群
You can use proc means as below,

data a;
input x y;
cards;
1 2
1 1
1 1
2 2
2 2
;
proc means data=a ;
class x y;
output out=b(where=(_type_ in (1,2)) rename=(_freq_=n));
run;

proc print data=b;
run;

使用道具

10
yuerqieqie 发表于 2013-12-6 00:21:00 |只看作者 |坛友微信交流群
proc sql;
        create table b as
        select 'x' as var, x as cat, count(*) as n
        from a group by 1, 2
        union
        select 'y' as var, y as cat, count(*) as n
        from a group by 1,2;
quit;

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-9-21 08:55