I have a small issue with my code :I am trying to create a data set that has
1)12 variables corresponding to each month,
2) that contain the price data for each combination of item and year.
my short datalines are
year month item price
2008 1 bread 1.00
2008 2 bread 1.02
2008 3 bread 1.03
2008 1 Eggs 1.40
2008 2 Eggs 1.45
2008 3 Eggs 1.33
and so on for next five years 2009,2010,2011,2012 with 12 months and having milk as third item and have different prices for each month and each year,
my code is
data aveprices_latest;
set sasdata.have;
proc sort data=aveprices_latest;
byitem;
run;
proc means data=aveprices_latest;
class year;
by item;
var price;
run;
proc tabulate data=aveprices_latest;
class item Year Month;
var price;
table item*Year, Month*Price;
run;
My output is showing data table of averages, whereas I want the price data -not sums of the price data.
I want to change the structure of the data table to make it have one column of data for each month.showing the combination of items and year.
Can you advise which part of my code need change?
data have;
input year month item $ price;
cards;
2008 1 bread 1.00
2008 2 bread 1.02
2008 3 bread 1.03
2008 1 Eggs 1.40
2008 2 Eggs 1.45
2008 3 Eggs 1.33
;
run;
proc sort data=have;by year item;run;
proc transpose data=have out=want;
by year item;
id month;
var price;
run;
data have;
input year month item $ price;
cards;
2008 1 bread 1.00
2008 2 bread 1.02
2008 3 bread 1.03
2008 1 Eggs 1.40
2008 2 Eggs 1.45
2008 3 Eggs 1.33
;
run;
proc sort data=have;by year item;run;
proc transpose data=have out=want;
by year item;
id month;
var price;
run;
I don't understand your second question. data have; input year month item $ price; cards; 2008 1 bread 1.00 2008 2 bread 1.02 2008 3 bread 1.03 2008 1 Eggs 1.40 2008 2 Eggs 1.45 2008 3 Eggs 1.33 ; run; proc format; value fmt 1='Jan' 2='Feb' 3='Mar'; run; proc sort data=have;by year item;run; proc transpose data=have out=want(drop=_name_ _label_); by year item; id month; format month fmt.; var price; run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.