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

Hello everyone, 

I have the below dataset :

data have ;
length brand Region1 Region2 Region3 $4;
input brand Region1 Region2 Region3 Units;
datalines;
Audi ON BC ON 25
Audi BC ON AB 30
Audi AB BC ON 20
Audi ON ON BC 10

BMW ON BC ON 25
BMW BC ON ON 30
BMW AB BC BC 20
BMW ON BC AB 10
;

 

I want the output as :

Capture.PNG

 

I was thinking : select brand,region3,sum(units) as total_sales from have group by 1,2;

But this way... I will have to run thrice(region1/2/3) and then merge ...

 

Is there an efficient way to do ?

 

Thank you!!!!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @new_sas_user_4  what you need to think is restucture , sum and restructure again. That's the algorithm

 



data have ;
length brand Region1 Region2 Region3 $4;
input brand Region1 Region2 Region3 Units;
datalines;
Audi ON BC ON 25
Audi BC ON AB 30
Audi AB BC ON 20
Audi ON ON BC 10
BMW ON BC ON 25
BMW BC ON ON 30
BMW AB BC BC 20
BMW ON BC AB 10
;
/*Wide to Long*/
data temp;
 set have;
 array t region:;
 length vn region $32;
 do over t;
  vn=vname(t);
  Region=t;
  output;
 end;
 drop region1-region3; 
run;
/*Summarize*/
proc summary data=temp nway completetypes;
class brand region vn;
var units;
output out=temp2(drop=_:) sum=;
run;

/*Tranpose back to wide as you wanted it*/
proc transpose data=temp2 out=want(drop=_:);
by  brand region ;
id vn;
var units;
run;

View solution in original post

4 REPLIES 4
ballardw
Super User

Do you expect a data set for further analysis or a report for people to read?

 

And describe the rule for Region (not currently a variable in your data set) and the Units variables. It may be obvious to you since it is your problem but not to others.

 

And did this format data come from summarizing something else? It might be easier to work with that source for some things.

ed_sas_member
Meteorite | Level 14

Hi @new_sas_user_4 

 

Here is an attempt to do this

proc sort data=have out=have_sorted;
	by brand units;
run;

proc transpose data=have_sorted out=have_exp (rename= (col1=region)) name=regionID;
	var Region:;
	by brand Units;
run;

proc sort data=have_exp;
	by brand region;
run;

proc means data=have_exp noprint;
	var units;
	class brand region regionID;
	ways 3;
	output out=have_sum (drop = _type_ _freq_) sum=sum_units;
run;

proc transpose data=have_sum out=have_tr (drop=_name_) suffix=_units;
	var sum_units;
	id regionID;
	by brand region;
run;
novinosrin
Tourmaline | Level 20

Hi @new_sas_user_4  what you need to think is restucture , sum and restructure again. That's the algorithm

 



data have ;
length brand Region1 Region2 Region3 $4;
input brand Region1 Region2 Region3 Units;
datalines;
Audi ON BC ON 25
Audi BC ON AB 30
Audi AB BC ON 20
Audi ON ON BC 10
BMW ON BC ON 25
BMW BC ON ON 30
BMW AB BC BC 20
BMW ON BC AB 10
;
/*Wide to Long*/
data temp;
 set have;
 array t region:;
 length vn region $32;
 do over t;
  vn=vname(t);
  Region=t;
  output;
 end;
 drop region1-region3; 
run;
/*Summarize*/
proc summary data=temp nway completetypes;
class brand region vn;
var units;
output out=temp2(drop=_:) sum=;
run;

/*Tranpose back to wide as you wanted it*/
proc transpose data=temp2 out=want(drop=_:);
by  brand region ;
id vn;
var units;
run;
new_sas_user_4
Obsidian | Level 7

Thanks a lot 🙂 @novinosrin @ed_sas_member @ballardw 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 517 views
  • 2 likes
  • 4 in conversation