BookmarkSubscribeRSS Feed
afs
Calcite | Level 5 afs
Calcite | Level 5

I am trying to get the maximum no of passengers in greyhound and megabus & abc routes to different cities . 

If i take one city Washington-WA and there are many buses going in different years to WA with different no of passenger(WAP).I have 20,000 of data set , just giving a short form,As my code is not working.

 

Bus             WAP            Year

Greyhound   4220           2000

Megabus      3200           2000

abc                15 00          2000

Greyhound   4200           2001

Megabus      3290           2001

abc                1598           2001

Greyhound   5200           2002

Megabus      3250           2002

abc                2000           2002

 

How to have one observation for the Bus variable with max passengers for the Year 2001.

I want to create a variable that represents the no of passengers per Bus for Year 2001 and which Bus. If anybody can help , and also can help me change that code to macro so that i can use any number of city, as i have 16 cities data. I will be very thanksful if anybody help me in getting the code right.

 

proc means nolabels data=WAP Max;
class Bus Year;
output out=maxp;
run;

proc print data=maxp;
where _stat_= MAX Year=2001;
var Bus Year WAP;
run;

 

 

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

I am having a bit of trouble understanding this, could you post what your desired output looks like? 🙂

afs
Calcite | Level 5 afs
Calcite | Level 5
1) i want to have data set result showing for the year 2001 which Bus had maximum no of passengers.

Can i get the above result please. Then i can run and ask again to discuss what i need.As data i have got is messy and i am trying to sort it. With your help i can get at least maximum no of people travelling in 2001 by which Bus.
thanks
Kurt_Bremser
Super User
data have;
input bus :$20. wap year;
cards;
Greyhound   4220           2000
Megabus      3200           2000
abc                1500          2000
Greyhound   4200           2001
Megabus      3290           2001
abc                1598           2001
Greyhound   5200           2002
Megabus      3250           2002
abc                2000           2002
;
run;

proc sql;
create table want as
select bus, wap
from have
where year = 2001
having wap = max(wap)
;
quit;
afs
Calcite | Level 5 afs
Calcite | Level 5
Can i please get in SAS as i have to make macros also then . I hope i am not bothering you much.Thanks
RW9
Diamond | Level 26 RW9
Diamond | Level 26

To get a good answer to your question, follow some general tips on posting:

- Post test data, in the form of a datastep

- Post example output you require

- Try to keep requirements to exactly what the question is about

 

Ksharp
Super User
For the legitimate sas data step code , could like:

proc sort data=have out=temp;
 by bus year wap;
run;
data want;
 set temp;
 by bus year;
 if last.year;
run;




mkeintz
PROC Star

You have not yet responded with an actual example of the result you wish to generate, so we are all trying to read your mind.  Here is my guess.  It uses the PROC MEANS that you have chosen.

 

Notes

  1. You will only get the maximum WITHIN class.  So use only year as a class variable, to get max within year.

  2. You want to use BUS as the id variable for the maximum passengers - so examine the MAXID clause in the output statement.

  3. Because the OUTPUT statement is being used to give names to statistics (i.e. maxwap) and id variables (maxwapbus), it doesn't generate one obs per stat per class.  It generates one obs per class, with several variables (one per stat).

  4. if there are two bus lines with maximum passengers for a given year, you will only get the name of the first one encountered in data set wap.

 

proc means data=wap nway noprint;
  class year;
  var  wap;
  output out=want maxid(wap(bus))=maxwapbus  max=maxwap;
run;

proc print data=want;
  var year maxwapbus maxwap;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 1089 views
  • 2 likes
  • 6 in conversation