BookmarkSubscribeRSS Feed
lkhadr
Calcite | Level 5

Hello!

I am trying to make a dataset that has an column N based on the values of two other variables grouped by the category "police department". 

More specifically, I have a dataset with individual ID, police department the individual(s) work at (there are 5 total in the dataset), one variable that asks if they agree or disagree with a statement regarding drug addition before a training (1=Strongly Disagree, 2=Disagree, 3=Neutral, 4=Agree, 5=Strongly Agree), and then another variable that asks the same question regarding drug addiction AFTER training. I ultimately want to create a dataset that shows how many police officers by police department said "strongly disagree" before and then "agree" after, "strongly disagree" before and then "strongly agree" after, etc. basically every combination of the 5 possible responses before versus 5 possible responses after. 

My dataset looks like this:

IDPOLICE_DEPTATT_PREATT_POST
1A12
2A23
3A23
4A51
5A51
6B33
7B33
8B33
9B21
10B21
11 C44
12C44

      

But I want this:

POLICE_DEPTATT_PREATT_POSTCOUNT
A121
A232
A512
B333
B212
C441

 

I realize PROC FREQ DATA=have; TABLE ATT_PRE*ATT_POST BY POLICE_DEPT; RUN would get me this data, but I want it in dataset form so I can use it to make an alluvial plot. 

I tried playing around with PROC SQL queries but they didn't work like this one:

PROC SQL;

CREATE TABLE want AS

SELECT police_dept, att_pre, att_post, count(police_dept)

FROM have

GROUP BY police_dept;

QUIT;

 

Thank you!

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

proc freq allows you to create a data set. Look at the output statement;

Tom
Super User Tom
Super User

So use PROC FREQ.

PROC FREQ DATA=have;
   BY POLICE_DEPT; 
   TABLES ATT_PRE*ATT_POST / noprint out=want ;
RUN;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 950 views
  • 1 like
  • 3 in conversation