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:
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;
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;
Would someone like to kindly show me how I should create the macro variable so as to subset the location variable successfully?
Thank you!
Hello,
Why do you want to create a macro variable if you know you want to filter by Dallas?
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.
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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.