BookmarkSubscribeRSS Feed
GregG
Quartz | Level 8

I am trying to get demographic counts from a dataset.

The below example is just a portion of the dataset, but contains most of what I am trying to measure.

For each section of a class, I want to get the number of the different ethnicities, genders, colleges (to which the student belongs) and classification of the student.

Right now my method is rather "clunky" but it works.

I've tried a combination of arrays and do loops, but it didn't seem to like what I was trying.

Thank you for your time.

data have;

length id $2 composite $12 ethnic $1 gender $1 college $2 class $2;

    infile datalines dlm=',' dsd;

    input id $ composite $ ethnic $ gender $ college $ class $;

return;

datalines;

"01","MATH*4583*01",1,"M","AS","FR"

"02","MATH*4583*01",5,"F","AS","SO"

"03","MATH*4583*01",2,"M","HS","JR"

"04","MATH*4583*01",4,"F","PE","SR"

"05","MATH*4583*01",3,"F","N","PB"

"06","MATH*4583*01",6,"M","G","FR"

"07","MATH*4583*01",7,"M","G","FR"

"01","MATH*4583*02",1,"M","AS","FR"

"04","MATH*4583*02",4,"F","PE","SR"

"02","MATH*4583*02",1,"F","AS","SO"

"08","MATH*4583*02",8,"M","N","PB"

;

run;

proc sort data=have;

    by composite;

run;   

/*    This works, but surely it can be done better    */

data want;

    set have;

    by composite;

   

    if first.composite then do;

        section_headcount = 0;

        eth_1 = 0; eth_2 = 0; eth_3 = 0; eth_4 = 0;

        eth_5 = 0; eth_6 = 0; eth_7 = 0; eth_8 = 0;

        m = 0; f = 0;

        as = 0; hs = 0; pe = 0; n = 0; g = 0;

        fr = 0; so = 0; jr = 0; sr = 0; pb = 0;

    end;

    section_headcount + 1;

    if ethnic = '1' then eth_1 + 1;

    if ethnic = '2' then eth_2 + 1;

    if ethnic = '3' then eth_3 + 1;

    if ethnic = '4' then eth_4 + 1;

    if ethnic = '5' then eth_5 + 1;

    if ethnic = '6' then eth_6 + 1;

    if ethnic = '7' then eth_7 + 1;

    if ethnic = '8' then eth_8 + 1;

    if gender = 'M' then m + 1;

    if gender = 'F' then f + 1;

    if college = 'AS' then as + 1;

    if college = 'HS' then hs + 1;

    if college = 'PE' then pe + 1;

    if college = 'N' then n + 1;

    if college = 'G' then g + 1;

    if class = 'FR' then fr + 1;

    if class = 'SO' then so + 1;

    if class = 'JR' then jr + 1;

    if class = 'SR' then sr + 1;

    if class = 'PB' then pb + 1;

    if last.composite;

    drop ethnic id gender college class;

run;

2 REPLIES 2
ballardw
Super User

If you want counts then look to a report procedure. Assuming your variable composite is what you refere to as section of class then

proc freq data = have;

     tables composite *( ethnic  gender college  class) / norow nocol nopercent ;

run;

might give you what you are looking for.

If you need a dataset then provide how it should look.

stat_sas
Ammonite | Level 13

Here is another way of doing it

proc tabulate data=have;

class composite ethnic gender college class;

table ethnic*gender*college all='Total',class*N=' ';

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 712 views
  • 0 likes
  • 3 in conversation