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

I'm working on creating a summary report using proc report. below is the sample of my dataset

 

categorymonthcount
category1201801245
category1201802335
category120180312
category220180178
category220180265
category220180334
category320180176
category320180255
category320180388

 

I want to create something like this,

 

 category1category2Percentage_increasecategory2category3percentage_increase
20180124578-0.6816326537876-0.025641026
20180233565-0.8059701496555-0.153846154
20180312341.83333333334881.588235294

 

my code is,


proc report data = membership_data;
column month category,count Percentage_change1 Percentage_change2;

define month / group ;
define category / across ;
define count/' ';

define Percentage_change1/computed format=percent8.2;
compute Percentage_change1;
Percentage_change1 = (_c3_-_c2_)/_c2_;

endcomp;


define Percentage_change2/computed format=percent8.2;
compute Percentage_change2;
Percentage_change2 = (_c4_-_c3_)/_c3_;
endcomp;
run;

 

after running above proc report, I end up having summary table like this

 

 category1category2category3Percentage_increase1percentage_increase2
2018012457876-0.681632653-0.026315789
2018023356555-0.805970149-0.181818182
2018031234881.8333333330.613636364

 

 my goal is to use the column 'category2' twice and rearrange the order. how to implement this. 

Any inputs are appreciated

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

It is not right way to do this by PROC REPORT.

 

data have;
input category : $40.	month	count;
cards;
category1	201801	245
category1	201802	335
category1	201803	12
category2	201801	78
category2	201802	65
category2	201803	34
category3	201801	76
category3	201802	55
category3	201803	88
;
proc sort data=have out=have1;
by month;
run;
data temp;
 set have1;
 by month;
 output;
 dif=dif(count);lag=lag(count);
 if first.month=0 then do;
  _count=count;_category=category;
  category=catx('_',category,'Percentage_increase');
  count=-dif/lag;
  output;
  if  last.month=0 then do;
  count=_count;category=cats(_category,'2');
  output;
  end;
 end;
 drop _: dif lag;
run;
proc transpose data=temp out=want;
by month;
id category;
var count;
run;

View solution in original post

3 REPLIES 3
Ksharp
Super User

It is not right way to do this by PROC REPORT.

 

data have;
input category : $40.	month	count;
cards;
category1	201801	245
category1	201802	335
category1	201803	12
category2	201801	78
category2	201802	65
category2	201803	34
category3	201801	76
category3	201802	55
category3	201803	88
;
proc sort data=have out=have1;
by month;
run;
data temp;
 set have1;
 by month;
 output;
 dif=dif(count);lag=lag(count);
 if first.month=0 then do;
  _count=count;_category=category;
  category=catx('_',category,'Percentage_increase');
  count=-dif/lag;
  output;
  if  last.month=0 then do;
  count=_count;category=cats(_category,'2');
  output;
  end;
 end;
 drop _: dif lag;
run;
proc transpose data=temp out=want;
by month;
id category;
var count;
run;
Jagadishkatam
Amethyst | Level 16

Alternatively, please try

 

data have;
input category:$12.	month	count;
cards;
category1 201801 245
category1 201802 335
category1 201803 12
category2 201801 78
category2 201802 65
category2 201803 34
category3 201801 76
category3 201802 55
category3 201803 88
;

proc sort data=have;
by month category;
run;

data want;
set have;
output;
by  month category;
new=lag(count);
if first.month then new=.;
if category='category2' then do;
category='_category2_';
output;
end;
if new ne . then do;
perct=(count-new)/new;
category='perct'||compress(category,,'kd');
output;
end;
run;

proc transpose data=want out=want2;
by month;
id category;
var count;
run;
Thanks,
Jag

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 579 views
  • 0 likes
  • 3 in conversation