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!
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.
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.
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;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: