BookmarkSubscribeRSS Feed
aabbccwyt
Obsidian | Level 7

Hi, please help me with the code for the following problem.

 

Create a temporary SAS table (I named it sportoutput), read in the worksheet (Sport_List)from the library. Subset the data for a given sport of your choice and use a macro variable to specify the sport. Also, subset data where Sport is Football, Soccer, Basketball or Table Tennis. 

 

Here is what I have so far:

data sportoutput;
        set Sport_List;
run;

%Let Sport=Basketball;
        proc print data=Sport_List;
where Sport="Basketball";
run;

 

Thank you so much!

 

3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

The way you point to the excel worksheet is wrong. You must define a library. Read your course notes again.

sustagens
Pyrite | Level 9
/*This subsets data where Sport is Football, Soccer, Basketball or Table Tennis*/
data Sport_List_Football Sport_List_Soccer Sport_List_Basketball Sport_List_Table_Tennis;
set Sport_List;
if Sport="Football" then output Sport_List_Football;
if Sport="Soccer" then output Sport_List_Soccer;
if Sport="Basketball" then output Sport_List_Basketball;
if Sport="Table Tennis" then output Sport_List_Table_Tennis;
run;
Reeza
Super User

PRINT only displays your data, subset your data means create a new dataset with certain, specific, records from the main data set.

Your use of macro variables is incorrect.

 

Here's a tutorial on filtering data:

https://video.sas.com/detail/video/4573016761001/filtering-a-sas-table-in-a-data-step?autoStart=true...

 

And here's a tutorial on macros/macro variables:

 

UCLA introductory tutorial on macro variables and macros

https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/

Tutorial on converting a working program to a macro

This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it 🙂 https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md

Examples of common macro usage

https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Ap...

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 932 views
  • 0 likes
  • 4 in conversation