DATA Step, Macro, Functions and more

report of data

Accepted Solution Solved
Reply
Contributor afs
Contributor
Posts: 28
Accepted Solution

report of data

[ Edited ]

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?


Accepted Solutions
Solution
‎11-13-2016 12:34 AM
Super User
Posts: 9,681

Re: report of data

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;

View solution in original post


All Replies
Solution
‎11-13-2016 12:34 AM
Super User
Posts: 9,681

Re: report of data

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;
Contributor afs
Contributor
Posts: 28

Re: report of data

I am getting the variables as col1 col2 col3 col4 for the months , can i change it to Jan feb March april and so on , moreover the list of variables is
year item -Name- _Lable- Col1 Col 2 Col 3 -----
under the _Name getting all obs as price and under Lable getting Average price ...Can we delete price and average price obs and just make a title as Average price .
Super User
Posts: 9,681

Re: report of data

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;

Contributor afs
Contributor
Posts: 28

Re: report of data

Thanks.. It worked....
Contributor afs
Contributor
Posts: 28

Re: report of data

Dear Ksharp-I am getting the variables as col1 col2 col3 col4 for the months , can i change it to Jan feb March april and so on , moreover the list of variables is
year item -Name- _Lable- Col1 Col 2 Col 3 -----
under the _Name getting all obs as price and under Lable getting Average price ...Can we delete price and average price obs and just make a title of the table as Average prices
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 225 views
  • 1 like
  • 2 in conversation