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

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 810 views
  • 0 likes
  • 3 in conversation