BookmarkSubscribeRSS Feed
xtan2
Calcite | Level 5

There is a question:

 

Create a Macro variable for Dallas in the Location variable of Data: ALL.
Use this macro variable to subset the ‘ALL’ excel data inside the proc SQL to select variables student_name,
teacher, fee, days, paid, location. Write a Proc SQL query to find the ‘average fee’ and ‘total fee’
by ‘paid’ variable for Dallas.

This data set of All is like:

xtan2_1-1615816973947.png

Using this following Proc SQL procedure the goal is easily achieved:

Proc SQL;
Select student_name, fee, days, paid, location,
mean(fee) as avg_fee label = 'average fee',
sum(fee) as tot_fee label = 'total fee'
From All
Where location = 'Dallas' and Paid = 'Y';
Quit;

xtan2_2-1615817163543.png

However, when I tried to use the following code to create a macro variable to subset the location variable into 'Dallas', it did not work:

 

Proc SQL;
Select student_name, fee, days, paid,
mean(fee) as avg_fee label = 'average fee',
sum(fee) as tot_fee label = 'total fee', location into :Dallas
From All
Where Paid = 'Y';
Quit;

xtan2_3-1615817289752.png

Would someone like to kindly show me how I should create the macro variable so as to subset the location variable successfully?

 

Thank you!

 

3 REPLIES 3
Athenkosi
Obsidian | Level 7

Hello,

 

Why do you want to create a macro variable if you know you want to filter by Dallas?

xtan2
Calcite | Level 5

Hello. Athenkosi,

 

This is because I have to satisfy what the instructor wants.

 

The question is 

Create a Macro variable for Dallas in the Location variable of Data ALL.
Use this macro variable to subset the ‘ALL’ excel data inside the proc SQL to select variables student_name,
teacher, fee, days, paid, location. Write a Proc SQL query to find the ‘average fee’ and ‘total fee’
by ‘paid’ variable for Dallas.

xtan2
Calcite | Level 5

 I got it! Also, I correct a mistake:

 

The data set should be classified by 'Pait', not 'Paid' = 'Y'.

 

 

%let loctn = Dallas;
Proc SQL;
Select student_name, fee, days, paid, location,
mean(fee) as avg_fee label = 'average fee',
sum(fee) as tot_fee label = 'total fee'
From All
Where location = "&loctn"
Group by paid;
Quit;

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
  • 1261 views
  • 0 likes
  • 2 in conversation