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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 4 replies
  • 2724 views
  • 1 like
  • 3 in conversation