BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
LMSSAS
Quartz | Level 8

Good monring, I am trying to subtract two numeric variables (visual is below my code )from two columns, then get the percentage in another column. I want to create a column with the difference, then another column with the percentage differernce. Can someone please look at my code and tell me how to solve for this? I am getting the same answers for all rows, i added the group by and that didn't correct it. 

PROC TRANSPOSE DATA = YOY OUT= OUTPUT (drop=_name_);
BY  'Submitt Date'n ;
ID  Year;
var AEP_Sales
;RUN;

Proc sql; create table AEP_Compare as
select distinct *,
sum(2021-2022) as Difference
From Output
group by 'Submitt Date'n
;quit;

Proc sql; create table AEP_CompareF as
select distinct *,
Difference/2021 as'Percentage Difference'n 
From AEP_Compare
group by 'Submitt Date'n
;quit;

LMSSAS_0-1667570309826.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
LMSSAS
Quartz | Level 8

Please disregard my original question. i was able to solve for this using, it was formatting issues. 

Proc sql; create table AEP_Compare as
select distinct *,
'2021'n-'2022'n as Difference,
('2021'n-'2022'n)/'2021'n as'Percentage Difference'n
From Output
group by 'Submitt Date'n,'2021'n, '2022'n
;quit;

View solution in original post

4 REPLIES 4
LMSSAS
Quartz | Level 8

Please disregard my original question. i was able to solve for this using, it was formatting issues. 

Proc sql; create table AEP_Compare as
select distinct *,
'2021'n-'2022'n as Difference,
('2021'n-'2022'n)/'2021'n as'Percentage Difference'n
From Output
group by 'Submitt Date'n,'2021'n, '2022'n
;quit;
ballardw
Super User

You have told the Sum function to "sum" the value 2021-2022, which is ALWAYS -1. SAS doesn't use variable names like 2021. Non-standard names must use the '2021'n nomenclature, just like your 'Submitt date'n. 

 

 

Tom
Super User Tom
Super User

Coding will be much easier if you use NORMAL variable names.

So if you want the TRANSPOSE step this way:

PROC TRANSPOSE DATA = YOY prefix=YEAR_ 
  OUT= OUTPUT (drop=_name_ rename=('Submitt Date'n=Submit_Date ))
; BY 'Submitt Date'n ; ID Year; var AEP_Sales; RUN;

Then OUTPUT will the variables SUBMIT_DATE YEAR_2021 YEAR_2022 etc instead of the goofy names that are causing you so much trouble. 

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!

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
  • 2562 views
  • 1 like
  • 3 in conversation