楼主: 13274118709
4461 15

[程序分享] 如何用sas程序对每个观测求满足条件的个数 [推广有奖]

11
流水不朽 发表于 2017-12-6 09:49:53
DATA tmp;
INPUT FID$4. YEAR 8.;
CARDS;
1111 1988
1111 1987
1112 1955
1112 1961
1112 1948
1112 1987
1113 1988
1114 1955
1114 1985
1115 1973
1115 1977
1115 1998
1115 2003
;
RUN;

/*用于值映射,减少if else 代码*/
proc format;
        value ftyear low-<1949='c'
                                 1949-<2000='a'
                                 2000-high='o'
        ;
run;

/*计算频数,可避免过多的 count语句*/
proc freq data=tmp;
        table fid*year/out=tmp_1 noprint;
        format year ftyear.;
run;

/*按照输出表格样式输出,把纵向的值,变为横向*/
proc transpose data=tmp_1 out=tmp_2(drop=_NAME_ _label_) /*PREFIX=cs_*/;
        id year;
        var count;
        by fid;
        idlabel year;
run;

/*把本身确实的,变为0*/
data        tmp_3;
        set tmp_2;
  array _num_ _numeric_;
           do i=1 to dim(_num_);
                 if _num_{i}=. then
                   _num_{i}=0;
         end;
   drop i;
run;

12
lovexialulu 发表于 2017-12-6 15:53:35
  1. proc sql noprint;
  2. create table b as select distinct fid ,
  3. count(case when .<year<1949  then year else . end) as c,
  4. count(case when 1949<=year<=2000  then year else . end) as a,
  5. count(case when year>2000  then year else . end) as o
  6. from a group by fid order by fid;
  7. run;
复制代码

13
superguy333 发表于 2017-12-6 19:55:35
lovexialulu 发表于 2017-12-6 15:53
不错,更简单!

14
13274118709 发表于 2017-12-7 16:53:38
lovexialulu 发表于 2017-12-6 15:53
谢谢~~

15
13274118709 发表于 2017-12-7 16:54:17
lovexialulu 发表于 2017-12-6 15:53
大神,我在做一份对调查问卷的分析,其中“风险偏好”这一变量的观测值缺失状况严重,但这个变量十分重要,想设置变量answer来考虑这种缺失情况以便减少信息损失(如果没有缺失为1,否则为0),将answer和“风险偏好”的交互项放在logisit回归方程中,用sas要怎么做?

16
13274118709 发表于 2017-12-7 16:55:50
superguy333 发表于 2017-12-5 19:16
DATA X1;
INPUT FID$4. YEAR 8.;
CARDS;
大神,我在做一份对调查问卷的分析,其中“风险偏好”这一变量的观测值缺失状况严重,但这个变量十分重要,想设置变量answer来考虑这种缺失情况以便减少信息损失(如果没有缺失为1,否则为0),将answer和“风险偏好”的交互项放在logisit回归方程中,用sas要怎么做?

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

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