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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.