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

Formatting help! I need to calculate avg and stddev of prices per quarter per year and present the price with a dollar format that will show the price having a dollar sign and two decimal spaces. Here is a sample of the data:

Year Month Gasprice

2003 1 1.47

2003 4 2.06

2003 8 1.89

2004 3 2.00

2004 8 2.69

2004 9 3.00

 

Here is what I have so far:

proc means data=work.gas
mean
stddev ;

class year month;

format month yyqd.
mean DOLLAR8.0;

var gasprice;
output out=work.gas2;
run;

 

I think i just need to figure out the correct formatting! 

1 ACCEPTED SOLUTION

Accepted Solutions
hashman
Ammonite | Level 13

@K_Wils15:

First, your format YYQD. assigned to Month won't work as intended. You need to create a variable where Year and Month are combined to represent a SAS date. Second, assign the DOLLAR format to the Gasprice, e.g.:

data have ;                                                                                                                             
  input year month gasprice ;                                                                                                           
  date = mdy (month, 1, year) ;                                                                                                         
  cards ;                                                                                                                               
2003 1 1.47                                                                                                                             
2003 4 2.06                                                                                                                             
2003 8 1.89                                                                                                                             
2004 3 2.00                                                                                                                             
2004 8 2.69                                                                                                                             
2004 9 3.00                                                                                                                             
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
proc means noprint data = have nway ;                                                                                                   
  class date ;                                                                                                                          
  var gasprice ;                                                                                                                        
  format date yyqd. gasprice dollar8.2 ;                                                                                                
  output out = stats (drop = _:) mean= stddev= / autoname ;                                                                             
run ;                                                          

Kind regards

Paul D.

  

View solution in original post

2 REPLIES 2
hashman
Ammonite | Level 13

@K_Wils15:

First, your format YYQD. assigned to Month won't work as intended. You need to create a variable where Year and Month are combined to represent a SAS date. Second, assign the DOLLAR format to the Gasprice, e.g.:

data have ;                                                                                                                             
  input year month gasprice ;                                                                                                           
  date = mdy (month, 1, year) ;                                                                                                         
  cards ;                                                                                                                               
2003 1 1.47                                                                                                                             
2003 4 2.06                                                                                                                             
2003 8 1.89                                                                                                                             
2004 3 2.00                                                                                                                             
2004 8 2.69                                                                                                                             
2004 9 3.00                                                                                                                             
;                                                                                                                                       
run ;                                                                                                                                   
                                                                                                                                        
proc means noprint data = have nway ;                                                                                                   
  class date ;                                                                                                                          
  var gasprice ;                                                                                                                        
  format date yyqd. gasprice dollar8.2 ;                                                                                                
  output out = stats (drop = _:) mean= stddev= / autoname ;                                                                             
run ;                                                          

Kind regards

Paul D.

  

mkeintz
PROC Star

If you need to format statistics by class levels, you'd be better off using PROC TABULATE:

 

Oops, Reading @hashman 's response made me realize I sent the wrong code. Here's the right code:

 

 

data GAS;
  input year month gasprice;
  gasdate=mdy(month,1,year);
datalines;
2003 1 1.47
2003 4 2.06
2003 8 1.89
2004 3 2.00
2004 8 2.69
2004 9 3.00
run; 
proc tabulate data=work.gas  noseps;
  class gasdate;
  format gasdate yyq6. ;
  var gasprice;
  table gasdate   /*Row identifiers */
        ,
        gasprice*(n*f=3.0 mean*f=dollar8.0  std*f=dollar8.2)  /*Column specification*/
        ;
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

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 428 views
  • 0 likes
  • 3 in conversation