DATA Step, Macro, Functions and more

Data for highest variable

Reply
Contributor afs
Contributor
Posts: 28

Data for highest variable

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;

 

 

PROC Star
Posts: 733

Re: Data for highest variable

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

Contributor afs
Contributor
Posts: 28

Re: Data for highest variable

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
Super User
Posts: 7,758

Re: Data for highest variable

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor afs
Contributor
Posts: 28

Re: Data for highest variable

Posted in reply to KurtBremser
Can i please get in SAS as i have to make macros also then . I hope i am not bothering you much.Thanks
Super User
Posts: 7,758

Re: Data for highest variable

This IS valid Base SAS code, believe me. Just run it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,758

Re: Data for highest variable

And if you tell me what needs to be made dynamic, I can at least give you hints how to do it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,942

Re: Data for highest variable

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

 

Super User
Posts: 10,018

Re: Data for highest variable

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;




Trusted Advisor
Posts: 1,018

Re: Data for highest variable

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;
Ask a Question
Discussion stats
  • 9 replies
  • 272 views
  • 2 likes
  • 6 in conversation