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

HI I have an input table like below:

 

WeekRecord_CountTotal Distinct Session IDTotal Distinct Submission NumberTotal Distinct POLICY_NOTotal Distinct DIVISION_NOTotal Distinct Effective DateSum_TOT_TECH_PREMSum_TOT_TARGET_PREM
12NOV2018416321295466125142348899.8133862781.3
05NOV20183162205003010100142312000.8142312000.8

 

I want to Calculate Difference and Percentage of difference, and my output table should look like :

WeekRecord_CountTotal Distinct Session IDTotal Distinct Submission NumberTotal Distinct POLICY_NOTotal Distinct DIVISION_NOTotal Distinct Effective DateSum_TOT_TECH_PREMSum_TOT_TARGET_PREM
12NOV2018416321295466125142348899.8133862781.3
05NOV20183162205003010100142312000.8142312000.8
Diffrence100101-20516-42536899-8449219.49
Percentage24.0%31.5%-69.5%34.8%-66.7%20.0%0.0%-6.3%
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep!!

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

At an untested guess:

proc sql;
  create table want as
  select *
  from   have
  union all
  select "Difference" as week,
         a.record_count-b.record_count as record_count,
         a.tds_id-b.tds_id as tds_id,
   ...
  from   (select * from have where week="12NOV2018") a
  full join (select * from have where week="05NOV2018") b
  on     1=1
  union all
  select "Percentage" as week,
         (a.record_count-b.record_count/a.record_count)*100 as record_count,
         (a.tds_id-b.tds_id/a.tds_id)*100 as tds_id,
   ...
  from   (select * from have where week="12NOV2018") a
  full join (select * from have where week="05NOV2018") b
  on     1=1;
quit;

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Post test data in the form of a datastep!!

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

At an untested guess:

proc sql;
  create table want as
  select *
  from   have
  union all
  select "Difference" as week,
         a.record_count-b.record_count as record_count,
         a.tds_id-b.tds_id as tds_id,
   ...
  from   (select * from have where week="12NOV2018") a
  full join (select * from have where week="05NOV2018") b
  on     1=1
  union all
  select "Percentage" as week,
         (a.record_count-b.record_count/a.record_count)*100 as record_count,
         (a.tds_id-b.tds_id/a.tds_id)*100 as tds_id,
   ...
  from   (select * from have where week="12NOV2018") a
  full join (select * from have where week="05NOV2018") b
  on     1=1;
quit;
DanielLangley
Quartz | Level 8

This is going to be difficult to replicate exactly as you wish to have different formats for the same columns. This does almost what you want.

 

data have;
infile datalines dsd delimiter=' ';
input week Record_Count
Total_Distinct_Session_ID
Total_Distinct_Submission_Number
Total_Distinct_POLICY_NO
Total_Distinct_DIVISION_NO
Total_Distinct_Effective_Date
Sum_TOT_TECH_PREM
Sum_TOT_TARGET_PREM;
attrib week informat = date9. format = date9.;

cards;
12NOV2018 416 321 295 46 6 125 142348899.8 133862781.3
05NOV2018 316 220 500 30 10 100 142312000.8 142312000.8
;
run;

/*Transpose the Data so it is easier to manipulate*/
PROC TRANSPOSE DATA=WORK.Have
	OUT=WORK.HaveTransposed(LABEL="Transposed WORK.HAVE")
	NAME=Source
	PREFIX=Week
	LABEL=Label
;
	ID week;
	VAR _NUMERIC_;
RUN;

data work.haveManipulated;
set work.HaveTransposed;
array Week[2] Week:;

	Difference = Week[1] - Week[2];
	Percentage = round(  100*Divide(Difference,Week[1])  ,0.1);

run;

PROC TRANSPOSE DATA=work.haveManipulated
	OUT=work.want(Drop = Week)
	NAME=Source
	LABEL=Label
;
	ID Source;
	VAR _Numeric_;

RUN;
Cynthia_sas
SAS Super FREQ
Hi:
If you were doing this in PROC REPORT or PROC TABULATE, both of those procedures would allow differing formats in the same column (where you show percentage in the row, but the other columns do not use a % sign).

PROC TABULATE won't calculate difference, however, the way you show, so I'd either pre-calculate the difference row or I'd use PROC REPORT. Even if I pre-calculated the row, I'd still use PROC REPORT to get the %sign for the Percentage row.
Cynthia

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 5073 views
  • 0 likes
  • 4 in conversation