BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Prashant_Ph
Fluorite | Level 6

Hi,

 

I have a data set and i want to sellect only highest Profit from attached file,

Ex:-  Highest profit from 2009 2010 2011 2012.

the data set have 9000 Observation from that i have sorted profit by if statment and i got folling record.

if profit = > 15000;

but i need only one one Highest profit from each year.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jklaverstijn
Rhodochrosite | Level 12

Hi @Prashant_Ph,

 

Not entirely certain what you want. So I take a stab guessing you want the max profit per year. You can use proc means for that. Assign the YEAR. format to the date to specifiy the required aggregation level of year.

 

data profit;
   infile cards dlm='#';
   length type $200;
   format pdate date9.;
   input pdate : mmddyy10. type $ profit;
cards;
1/6/2009#Delivery Truck#16413.8242
1/7/2009#Regular Air#18527.1684
1/22/2009#Delivery Truck#17506.2518
2/15/2009#Delivery Truck#15227.6924
3/21/2009#Regular Air#34399.4625
8/21/2009#Delivery Truck#15688.9425
1/3/2010#Delivery Truck#18052.4284
10/20/2010#Delivery Truck#15904.1736
5/22/2011#Delivery Truck#19200.9844
11/19/2011#Delivery Truck#18598.6608
12/21/2011#Delivery Truck#19200.9844
5/21/2012#Regular Air#21172.5384
8/7/2012#Delivery Truck#19952.96
10/16/2012#Regular Air#20065.9887
12/2/2012#Delivery Truck#15315.7884
12/12/2012#Delivery Truck#15321.409
;
proc means max;
   class pdate;
   format pdate year4.;
run;

This is the output:

The MEANS Procedure

Analysis Variable : profit  
pdate N Obs Maximum 
2009 6 34399.46 
2010 2 18052.43 
2011 3 19200.98 
2012 5 21172.54 

This should get you started. Proc means can create an output dataset if you like.

 

If I misinterpreted your question please elaborate and give a sample of the dersired output.

Regards,

- Jan.

 

View solution in original post

8 REPLIES 8
jklaverstijn
Rhodochrosite | Level 12

Hi @Prashant_Ph,

 

Not entirely certain what you want. So I take a stab guessing you want the max profit per year. You can use proc means for that. Assign the YEAR. format to the date to specifiy the required aggregation level of year.

 

data profit;
   infile cards dlm='#';
   length type $200;
   format pdate date9.;
   input pdate : mmddyy10. type $ profit;
cards;
1/6/2009#Delivery Truck#16413.8242
1/7/2009#Regular Air#18527.1684
1/22/2009#Delivery Truck#17506.2518
2/15/2009#Delivery Truck#15227.6924
3/21/2009#Regular Air#34399.4625
8/21/2009#Delivery Truck#15688.9425
1/3/2010#Delivery Truck#18052.4284
10/20/2010#Delivery Truck#15904.1736
5/22/2011#Delivery Truck#19200.9844
11/19/2011#Delivery Truck#18598.6608
12/21/2011#Delivery Truck#19200.9844
5/21/2012#Regular Air#21172.5384
8/7/2012#Delivery Truck#19952.96
10/16/2012#Regular Air#20065.9887
12/2/2012#Delivery Truck#15315.7884
12/12/2012#Delivery Truck#15321.409
;
proc means max;
   class pdate;
   format pdate year4.;
run;

This is the output:

The MEANS Procedure

Analysis Variable : profit  
pdate N Obs Maximum 
2009 6 34399.46 
2010 2 18052.43 
2011 3 19200.98 
2012 5 21172.54 

This should get you started. Proc means can create an output dataset if you like.

 

If I misinterpreted your question please elaborate and give a sample of the dersired output.

Regards,

- Jan.

 

MeganE
Pyrite | Level 9

If you want a subset dataset you can select off after the sort.

 

proc sort data=[my data];  by year profit; run;

 

data [my data2];

  set [my data];

  by year profit;

  if first.year and last.profit then output;

run;

 

If it's sorted by profit ascending per year, that should pull out the highest per year.

 

Reeza
Super User

@MeganE You should take last.year for the highest profit. 

 

proc sort data=[my data];  by year profit; run;
 
data [my data2];
  set [my data];
  by year;
  if last.year;
run;

 

 

MeganE
Pyrite | Level 9

Oh, look at that, i flipped them.  Yes, you're right!

jklaverstijn
Rhodochrosite | Level 12

@MeganE @Reeza there is no YEAR in the data that @Prashant_Ph attached.

MeganE
Pyrite | Level 9

Really?  I'll admit i didn't look b/c i'm not comfortable opening files like that from people i don't know out on the internet.  But wow.  The original post surely implied year was part of the data...

Reeza
Super User

@MeganE I agree about downloading files and year variable. It's relatively easy to calculate year anyways. Either use Year() function or year format. 

Prashant_Ph
Fluorite | Level 6

Am really thank full @jklaverstijn

 

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
  • 8 replies
  • 1675 views
  • 1 like
  • 4 in conversation