BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
afs
Calcite | Level 5 afs
Calcite | Level 5

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

5 REPLIES 5
Ksharp
Super User
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;
afs
Calcite | Level 5 afs
Calcite | Level 5
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 .
Ksharp
Super User
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;

afs
Calcite | Level 5 afs
Calcite | Level 5
Thanks.. It worked....
afs
Calcite | Level 5 afs
Calcite | Level 5
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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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