使用WPS中的给定列进行数据准备

时间:2014-04-28 15:14:49

标签: sas

我有一个包含两个性别和birth_date列的数据集,我需要输出的形式有两行,即男性和女性(性别),列为一年中的几个月。我如何在SAS中做到这一点?

假设:

Gender  Birthdate 
Male    01/10/1989
Female  02/12/1990

依此类推..我有大约100K行。

2 个答案:

答案 0 :(得分:0)

如果我理解了你的要求以及下面的结果集是否与你想要的输出相似......这里是我用来命名初始数据集的代码' YYY'

data new(drop=year1 MonthName birthdate) ; 
   set yyy;
   format new_col $14. MonthName year1 $12.;
   year1 = compress(year(birthdate));
   MonthName=put(birthdate,monname.);
   new_col = trim(MonthName)||trim(year1);
run;

proc sort data=new;
by gender new_col;
run;

proc sql;
create table new2 as
    select gender, New_col, count(new_col) as New_col2
  from new
  group by gender, New_col;
quit;

proc transpose data=new2 
 out=result 
 name=New_col; 
 by gender;
 id New_col; 
run; 

Data final;
  set result(drop=New_col);
run;

结果集:

enter image description here

答案 1 :(得分:0)

所以我假设你想要每个月出生的人数。

第一个数据集会创建一些测试数据。

data test;
format Gender $8. Birthdate date9.;
do i=1 to 5000;
    gender="MALE";
    Birthdate = today() + ceil(ranuni(123)*365);
    output;
end;
do i=1 to 5000;
    gender="FEMALE";
    Birthdate = today() + ceil(ranuni(123)*365);
    output;
end;
drop i;
run;

proc sort data=test;
by gender;
run;

data output(keep=gender month:);
set test;
by gender;
array Month_[12];
if first.gender then do;
    do i=1 to 12;
        month_[i] = 0;
    end;
end;

month_[month(birthdate)] + 1;

if last.gender then
    output;
run;

创建 enter image description here