BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kcmitche
Calcite | Level 5

Evening,

 

current code 

proc import datafile="/home/kcmitche/Basketball Data.xlsx"
out=BB dbms=xlsx replace;
run;

proc freq data=BB;
tables team_name*event_desc_id / crosslist Missing nocum nopercent;
run;

 

 

Question:

When doing a cross tabulation how can i tell SAS to only count how many times a certain event happened in the (even_desc_id)?

I want to count how many times a specific variable happens rather than all of them.

 

 

 

2nd question more advanced.

Is there a way to count how many times something happen (see above) based on what the row below in the data says?

 

thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@kcmitche wrote:

I'm trying to create a table that will show all the team_names.

then show how many Offensive rebounds they had (listed as 'O') in event_desc_id

that happened directly before a 2FGA or TO

 

Does that make any sense?

 

Essentially I need to figure out how many time a team had an offensive rebound that was directly followed by a certain event.  (The certain event can change) and I'd like to give percentages.

 


data inter;
set have;
if event_desc_id in ('2FGA','TO') and lag(event_desc_id) = 'O'
then output;
run;

proc freq data=inter;
table team_name;
run;

Completely untested, for lack of data.

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Use the macro provided in https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... to post an example of your dataset BB in datastep form, and an example what your expected output from proc freq should be.

Do NOT post the Excel file, as it is useless (security, firewalls) in most cases.

 

A variable "happens" in every observation, so I guess you meant how often certain values appear. This is what proc freq does.

 

For your second question, the lag() function might be the answer. But we can help more with example data.

kcmitche
Calcite | Level 5

I can't follow and understand how to create the data (from the link you provided)

I'm sorry I'm not as skilled in SAS as I'd like to be.

 

Screenshot of DATA

 

Maybe this screenshot will help with my data since you don't have to download anything.

ballardw
Super User

And what would the desired result from your posted example "data" be?

kcmitche
Calcite | Level 5

I'm trying to create a table that will show all the team_names.

then show how many Offensive rebounds they had (listed as 'O') in event_desc_id

that happened directly before a 2FGA or TO

 

Does that make any sense?

 

Essentially I need to figure out how many time a team had an offensive rebound that was directly followed by a certain event.  (The certain event can change) and I'd like to give percentages.

 

Kurt_Bremser
Super User

@kcmitche wrote:

I'm trying to create a table that will show all the team_names.

then show how many Offensive rebounds they had (listed as 'O') in event_desc_id

that happened directly before a 2FGA or TO

 

Does that make any sense?

 

Essentially I need to figure out how many time a team had an offensive rebound that was directly followed by a certain event.  (The certain event can change) and I'd like to give percentages.

 


data inter;
set have;
if event_desc_id in ('2FGA','TO') and lag(event_desc_id) = 'O'
then output;
run;

proc freq data=inter;
table team_name;
run;

Completely untested, for lack of data.

Kurt_Bremser
Super User

@kcmitche wrote:

I can't follow and understand how to create the data (from the link you provided)

I'm sorry I'm not as skilled in SAS as I'd like to be.


 

Maybe this screenshot will help with my data since you don't have to download anything.


It's not rocket science, just very basic Base SAS usage. Before embarking on anything more complicated with SAS, you should already have developed the necessary SAS skills. Besides, the whole thing is very well documented, so even very novice users can handle it. That is the express purpose of the macro, it is designed to assist newcomers here in providing sample data for their questions.

- download the .zip

- unpack the .sas contained within

- open in EG or Studio or SAS DMS editor (or open with any text editor and copy/paste from there into SAS)

- run it. The macro will be compiled for your current SAS session

Now you can use the macro to convert your dataset, as is described in the linked page.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 921 views
  • 0 likes
  • 3 in conversation