Hello,
So, my data is similar to this:
data employeesl;
input employee $ station $ gender $ race $ StationID;
datalines;
Smith west F B 124
Turner east M W 123
Porter north M W 125
William west F W 124
Lee south F B 126
Maine south M B 126
;
run;
I am looking to create a table similar to this:
Station | StationID | White Male | White Female | Black Male | Black Female |
East | 123 | 1 | 0 | 0 | 0 |
West | 124 | 0 | 1 | 0 | 1 |
North | 125 | 1 | 0 | 0 | 0 |
South | 126 | 0 | 0 | 1 | 1 |
All Stations | 999 | 2 | 1 | 1 | 2 |
Thank you!
With table as is, this is fairly close:
proc tabulate data=employeesl;
class station gender race stationID;
table (station*stationID all), race=''*gender=''*N='' / misstext='0';
run;
To have the gender/race headers all in one column and expanded, you can use formats and/or combine them into a single variable and then use the single variable instead of the race*gender in proc tabulate.
Is the table a SAS dataset or for a report, ie PDF?
data employeesl;
input employee $ station $ gender $ race $ StationID;
datalines;
Smith west F B 124
Turner east M W 123
Porter north M W 125
William west F W 124
Lee south F B 126
Maine south M B 126
;
run;
*formats to control display;
proc format;
value $ race_fmt 'B'='Black' 'W'='White' other='Other';
value $ gender_fmt 'F'='Female' 'M'='Male' other='Other';
run;
*generic tabulate;
proc tabulate data=employeesl;
class station gender race stationID;
table (station*stationID all), race=''*gender=''*N='' / misstext='0';
format gender $gender_fmt. race $race_fmt.;
run;
*combining categorical variables of race/gender;
data employeesl;
set employeesl;
category=catx(" ", put(race, $race_fmt.), put(gender, $gender_fmt.));
run;
*tabulate with category combined;
proc tabulate data=employeesl out=long;
class station stationID category;
table (station*stationID all), category=''*N='' / misstext='0';
run;
proc sort data=long;
by descending station stationId;
run;
*output in dataset;
proc transpose data=long out=wide (drop=_:);
by descending station stationid;
id category;
var N;
run;
*fill in missing and add ALL label;
data wide;
set wide;
array _vars _numeric_;
do over _vars;
if _vars=. then
_vars=0;
end;
if missing(station) then
do;
station='All';
call missing(stationID);
end;
run;
*display results;
proc print data=wide noobs;
run;
With table as is, this is fairly close:
proc tabulate data=employeesl;
class station gender race stationID;
table (station*stationID all), race=''*gender=''*N='' / misstext='0';
run;
To have the gender/race headers all in one column and expanded, you can use formats and/or combine them into a single variable and then use the single variable instead of the race*gender in proc tabulate.
Is the table a SAS dataset or for a report, ie PDF?
data employeesl;
input employee $ station $ gender $ race $ StationID;
datalines;
Smith west F B 124
Turner east M W 123
Porter north M W 125
William west F W 124
Lee south F B 126
Maine south M B 126
;
run;
*formats to control display;
proc format;
value $ race_fmt 'B'='Black' 'W'='White' other='Other';
value $ gender_fmt 'F'='Female' 'M'='Male' other='Other';
run;
*generic tabulate;
proc tabulate data=employeesl;
class station gender race stationID;
table (station*stationID all), race=''*gender=''*N='' / misstext='0';
format gender $gender_fmt. race $race_fmt.;
run;
*combining categorical variables of race/gender;
data employeesl;
set employeesl;
category=catx(" ", put(race, $race_fmt.), put(gender, $gender_fmt.));
run;
*tabulate with category combined;
proc tabulate data=employeesl out=long;
class station stationID category;
table (station*stationID all), category=''*N='' / misstext='0';
run;
proc sort data=long;
by descending station stationId;
run;
*output in dataset;
proc transpose data=long out=wide (drop=_:);
by descending station stationid;
id category;
var N;
run;
*fill in missing and add ALL label;
data wide;
set wide;
array _vars _numeric_;
do over _vars;
if _vars=. then
_vars=0;
end;
if missing(station) then
do;
station='All';
call missing(stationID);
end;
run;
*display results;
proc print data=wide noobs;
run;
/*Just for having some fun.*/
data employeesl;
input employee $ station $ gender $ race $ StationID;
datalines;
Smith west F B 124
Turner east M W 123
Porter north M W 125
William west F W 124
Lee south F B 126
Maine south M B 126
;
run;
proc sql;
select station,StationID,
sum(gender='M' and race='W') as White_Male,
sum(gender='F' and race='W') as White_Female,
sum(gender='M' and race='B') as Black_Male,
sum(gender='F' and race='B') as Black_Female
from employeesl
group by station,StationID
union all
select 'All Stations',999,
sum(gender='M' and race='W') as White_Male,
sum(gender='F' and race='W') as White_Female,
sum(gender='M' and race='B') as Black_Male,
sum(gender='F' and race='B') as Black_Female
from employeesl
;
quit;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.