Data Airtraffic_19;
set "/folders/myfolders/airtraffic.sas7bdat";
run;
proc contents data = airtraffic_19;
%Let MacroYear = 2003;
procedure means data = airtraffic_19;
var Year quarter Bosflights BosPassengers;
Output out = BosAirtraffic (Drop=_type_ _freq_)
sum (Bosflights) = Sumflights
Sum (BosPassengers) =SumPass;
by Airline;
where Year = &MacroYear.; *Using a macro variable to specify the year 2003;
run;
%Let MacroYear = 2000;
procedure means data = airtraffic_19;
var Year quarter Bosflights BosPassengers;
Output out = BosAirtraffic (Drop=_type_ _freq_)
Sum (Bosflights) = Sumflights
Sum (BosPassengers) =SumPass
MAX (BosPassengers) = MaxPass;
by Airline;
where Year = &MacroYear.; *Using a macro variable to specify the year 2003;
run;
Data Maxpass;
set BosAirTraffic;
PassengersPerF = Round (SumPass/SumFlights, 2); *Creating a variable that represents the number
of passengers per flight for that one year and airline rounded;
run;
proc sql;
create table MaxPass2 as
select *,
max(MaxPass) as MaxPass19
From Maxpass
having MaxPass EQ calculated MaxPass19
;
Quit;
***Is there a way I can forgoe the second dataset and do everything in the proc means step?
*Macro with parameters;
%Macro TopAirlines(Flights=, Passengers =,);
%Let MacroYear = 2000;
procedure means data = airtraffic_19;
Output out = "&Flights.AirTraffic" (Drop=_type_ _freq_)
Sum (&flights) = Sumflights
Sum (&Passengers) =SumPass
MAX (&Passengers) = MaxPass;
by Airline;
where Year = &MacroYear.; *Using a macro variable to specify the year 2003;
run;
%Mend TopAirlines;
%TopAirlines (Flights = DENFlights, Passengers = DENPassengers)
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
72
73 %TopAirlines (Flights = DENFlights, Passengers = DENPassengers);
ERROR: User does not have appropriate authorization level for library WC000002.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE MEANS used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
74
75 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
87
Add the following options to help you debug macros
Options mprint symbolgen;
Your output statement is incorrect, you don’t need quotes around the data set name.
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...
@petergomillion wrote:
Hi All,I've been tasked with building out a report with the following context:The United States Department of Transportation publishes statistics on many modes of transportation. The SAS data set called AIRTRAFFIC contains data on the number of flights and passengers leaving 12 major airports in the United States. The data set contains data from each airline, for each quarter, for 20 years. In addition to the variables for year, quarter, and airline, there are two variables representing number of flights and passengers for each of the 12 airports. The variable names for the flights and passengers all start with the three-letter airport code: ATL (Atlanta), BOS (Boston), DEN (Denver), DFW (Dallas Fort Worth), EWR (Newark), HNL (Honolulu), LAX (Los Angeles), MIA (Miami), ORD (Chicago), SAN (San Diego), SEA (Seattle), and SFO (San Francisco). The data are sorted by year, airline, and quarter.I've been able to generate code for the first few steps:8.1 Examine this SAS data set including the variable labels and attributes. For BOS (Boston), create a data set that contains variables for the sum of the flights and for the sum of the passengers over all quarters, for each airline for one selected year of your choice. Use a macro variable to specify the value of the selected year.
Data Airtraffic_19; set "/folders/myfolders/airtraffic.sas7bdat"; run; proc contents data = airtraffic_19; %Let MacroYear = 2003; procedure means data = airtraffic_19; var Year quarter Bosflights BosPassengers; Output out = BosAirtraffic (Drop=_type_ _freq_) sum (Bosflights) = Sumflights Sum (BosPassengers) =SumPass; by Airline; where Year = &MacroYear.; *Using a macro variable to specify the year 2003; run;
8.1 Use the data set from part a) to create another data set with one observation for the airline with the most passengers for the selected year. Create a variable that represents the number of passengers per flight for that one year and airline. Round this value to a whole number.
***Is there a way I can forgoe the second dataset and do everything in the proc means step?%Let MacroYear = 2000; procedure means data = airtraffic_19; var Year quarter Bosflights BosPassengers; Output out = BosAirtraffic (Drop=_type_ _freq_) Sum (Bosflights) = Sumflights Sum (BosPassengers) =SumPass MAX (BosPassengers) = MaxPass; by Airline; where Year = &MacroYear.; *Using a macro variable to specify the year 2003; run; Data Maxpass; set BosAirTraffic; PassengersPerF = Round (SumPass/SumFlights, 2); *Creating a variable that represents the number of passengers per flight for that one year and airline rounded; run; proc sql; create table MaxPass2 as select *, max(MaxPass) as MaxPass19 From Maxpass having MaxPass EQ calculated MaxPass19 ; Quit;
The last few steps are giving me some trouble:8.1 Convert your code for parts a) and b) into a macro so that it can be run for any airport. Call the macro once for each of the 12 airports.
*Macro with parameters; %Macro TopAirlines(Flights=, Passengers =,); %Let MacroYear = 2000; procedure means data = airtraffic_19; Output out = "&Flights.AirTraffic" (Drop=_type_ _freq_) Sum (&flights) = Sumflights Sum (&Passengers) =SumPass MAX (&Passengers) = MaxPass; by Airline; where Year = &MacroYear.; *Using a macro variable to specify the year 2003; run; %Mend TopAirlines; %TopAirlines (Flights = DENFlights, Passengers = DENPassengers)
I get the following error in the log:
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 72 73 %TopAirlines (Flights = DENFlights, Passengers = DENPassengers); ERROR: User does not have appropriate authorization level for library WC000002. NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE MEANS used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 74 75 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 87
I am using SAS University Edition if that means if that helps. I think the code should work but I'm not sure why it won't work. Thanks.
Add the following options to help you debug macros
Options mprint symbolgen;
Your output statement is incorrect, you don’t need quotes around the data set name.
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...
@petergomillion wrote:
Hi All,I've been tasked with building out a report with the following context:The United States Department of Transportation publishes statistics on many modes of transportation. The SAS data set called AIRTRAFFIC contains data on the number of flights and passengers leaving 12 major airports in the United States. The data set contains data from each airline, for each quarter, for 20 years. In addition to the variables for year, quarter, and airline, there are two variables representing number of flights and passengers for each of the 12 airports. The variable names for the flights and passengers all start with the three-letter airport code: ATL (Atlanta), BOS (Boston), DEN (Denver), DFW (Dallas Fort Worth), EWR (Newark), HNL (Honolulu), LAX (Los Angeles), MIA (Miami), ORD (Chicago), SAN (San Diego), SEA (Seattle), and SFO (San Francisco). The data are sorted by year, airline, and quarter.I've been able to generate code for the first few steps:8.1 Examine this SAS data set including the variable labels and attributes. For BOS (Boston), create a data set that contains variables for the sum of the flights and for the sum of the passengers over all quarters, for each airline for one selected year of your choice. Use a macro variable to specify the value of the selected year.
Data Airtraffic_19; set "/folders/myfolders/airtraffic.sas7bdat"; run; proc contents data = airtraffic_19; %Let MacroYear = 2003; procedure means data = airtraffic_19; var Year quarter Bosflights BosPassengers; Output out = BosAirtraffic (Drop=_type_ _freq_) sum (Bosflights) = Sumflights Sum (BosPassengers) =SumPass; by Airline; where Year = &MacroYear.; *Using a macro variable to specify the year 2003; run;
8.1 Use the data set from part a) to create another data set with one observation for the airline with the most passengers for the selected year. Create a variable that represents the number of passengers per flight for that one year and airline. Round this value to a whole number.
***Is there a way I can forgoe the second dataset and do everything in the proc means step?%Let MacroYear = 2000; procedure means data = airtraffic_19; var Year quarter Bosflights BosPassengers; Output out = BosAirtraffic (Drop=_type_ _freq_) Sum (Bosflights) = Sumflights Sum (BosPassengers) =SumPass MAX (BosPassengers) = MaxPass; by Airline; where Year = &MacroYear.; *Using a macro variable to specify the year 2003; run; Data Maxpass; set BosAirTraffic; PassengersPerF = Round (SumPass/SumFlights, 2); *Creating a variable that represents the number of passengers per flight for that one year and airline rounded; run; proc sql; create table MaxPass2 as select *, max(MaxPass) as MaxPass19 From Maxpass having MaxPass EQ calculated MaxPass19 ; Quit;
The last few steps are giving me some trouble:8.1 Convert your code for parts a) and b) into a macro so that it can be run for any airport. Call the macro once for each of the 12 airports.
*Macro with parameters; %Macro TopAirlines(Flights=, Passengers =,); %Let MacroYear = 2000; procedure means data = airtraffic_19; Output out = "&Flights.AirTraffic" (Drop=_type_ _freq_) Sum (&flights) = Sumflights Sum (&Passengers) =SumPass MAX (&Passengers) = MaxPass; by Airline; where Year = &MacroYear.; *Using a macro variable to specify the year 2003; run; %Mend TopAirlines; %TopAirlines (Flights = DENFlights, Passengers = DENPassengers)
I get the following error in the log:
1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 72 73 %TopAirlines (Flights = DENFlights, Passengers = DENPassengers); ERROR: User does not have appropriate authorization level for library WC000002. NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE MEANS used (Total process time): real time 0.00 seconds cpu time 0.00 seconds 74 75 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK; 87
I am using SAS University Edition if that means if that helps. I think the code should work but I'm not sure why it won't work. Thanks.
Thanks Mate! I will review links! Removing the quotes worked!
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.