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

Hi, 

 

I am trying to transform how my data is laid out. The data that I am starting with is formatted as such: 

ProductMetricActualForecastTotal
ASales101020
AProfit5510
BSales202040
BProfit151530

 

I am trying to transform my data to look like this:

ProductSales ActualSales ForecastSales TotalProfit ActualProfit ForecastProfit Total
A1010205510
B202040151530

 

I am newer to SAS, but have had success with transposing data in the past though i have not been able to get it to work with this dataset. 

 

Thank you in advance for the help,

Emily

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's one way:

 

data want;

merge have (where=(metric='Sales') rename=(actual=Sales_Actual forecast=Sales_Forecast total=Sales_Total))

have (where=(metric='Profit') rename=(actual=Profit_Actual forecast=Profit_Forecast total=Profit_Total));

by product;

drop metric;

run;

 

It assumes your data set is already sorted by PRODUCT.

View solution in original post

8 REPLIES 8
Astounding
PROC Star

Here's one way:

 

data want;

merge have (where=(metric='Sales') rename=(actual=Sales_Actual forecast=Sales_Forecast total=Sales_Total))

have (where=(metric='Profit') rename=(actual=Profit_Actual forecast=Profit_Forecast total=Profit_Total));

by product;

drop metric;

run;

 

It assumes your data set is already sorted by PRODUCT.

ecc14
Calcite | Level 5

Thank you so much Astounding! This worked for me.

 

novinosrin
Tourmaline | Level 20
data have;
input Product $	Metric $	Actual	Forecast	Total;
cards;
A	Sales	10	10	20
A	Profit	5	5	10
B	Sales	20	20	40
B	Profit	15	15	30
;

proc transpose data=have out=_have;
by product metric notsorted;
var Actual	Forecast	Total;
run;

proc transpose data=_have out=want;
by product;
var col1;
id metric _name_;
run;
Andygray
Quartz | Level 8

I don't think astounding solution is a proper automated solution. In my opinion that's hard coding and will not scale if the number of metric changes. 

Ksharp
Super User

OK. Merge Skill come to rescue. proposed by me,Matt,Arthur.T:

http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf

 

 

data have;
input Product $	Metric $	Actual	Forecast	Total;
cards;
A	Sales	10	10	20
A	Profit	5	5	10
B	Sales	20	20	40
B	Profit	15	15	30
;

proc sort data=have(keep=Metric) out=key nodupkey;
by Metric;
run;
data _null_;
 set key end=last;
 if _n_=1 then call execute('data want;merge ');
 call execute(catt('have(where=(Metric="',Metric,'") 
 rename=(Actual=',Metric,'_Actual Forecast=',Metric,'_Forecast Total=',Metric,'_Total))'));
 if last then call execute(';by Product;drop Metric;run;');
 run;
Ksharp
Super User

OK. Merge Skill come to rescue. proposed by me,Matt,Arthur.T:

http://support.sas.com/resources/papers/proceedings15/2785-2015.pdf

 

 

data have;
input Product $	Metric $	Actual	Forecast	Total;
cards;
A	Sales	10	10	20
A	Profit	5	5	10
B	Sales	20	20	40
B	Profit	15	15	30
;

proc sort data=have(keep=Metric) out=key nodupkey;
by Metric;
run;
data _null_;
 set key end=last;
 if _n_=1 then call execute('data want;merge ');
 call execute(catt('have(where=(Metric="',Metric,'") 
 rename=(Actual=',Metric,'_Actual Forecast=',Metric,'_Forecast Total=',Metric,'_Total))'));
 if last then call execute(';by Product;drop Metric;run;');
 run;
ecc14
Calcite | Level 5

Do you mind walking me through what you are doing here? I'm not sure I am following:

 

call execute(catt('have(where=(Metric="',Metric,'") 
 rename=(Actual=',Metric,'_Actual Forecast=',Metric,'_Forecast Total=',Metric,'_Total))'));
 if last then call execute(';by Product;drop Metric;run;')

 

Ksharp
Super User
This produce the Astounding's code .



data want;

merge have (where=(metric='Sales') rename=(actual=Sales_Actual forecast=Sales_Forecast total=Sales_Total))

have (where=(metric='Profit') rename=(actual=Profit_Actual forecast=Profit_Forecast total=Profit_Total));

by product;

drop metric;

run;


hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1692 views
  • 4 likes
  • 5 in conversation