Вопрос

I have a dataset with two columns having gender and birth_date, I need the output in such a form that there are two rows i.e. male and female (gender) and columns as months of a year. How do I do that in SAS?

Suppose:

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

and so on..i have around 100K rows.

Это было полезно?

Решение

So I assume you want the number of people born in each month.

The first data set creates some test data.

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;

Creates enter image description here

Другие советы

If I understood what you are asking and if the result set below looks like the output you wanted.. here is the code I used naming the initial data set as '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;

Result set:

enter image description here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top