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

Dear All,

I have searched the forum for the answer, but could not find something relevant to my query.

 

I have daily average data for two variables – the dataset keeps updating on daily basis (connected to live data source). For the calculation of a parameter, I need to get the average of both the variables, get their ratio and calculate the percentage increase or decrease over the period of year (also given as formula as follows).

 

I know that I need to setup Macro but how should it be? My questions are:

 

  1. How to convert daily average to yearly average for the variables in SAS Code? I want to have the macro, which automatically separates each year and calculate the average for the variables (as data flows)?

                               var1_avg_year123 = 123.456

                               var2_avg_year123 = 234.567

               Indicator_year123 = var1_avg_year123 / var2_avg_year123

2.  The calculated average is then used in given formula to calculate the percentage change in the Performance Indicator.

Perf_Indicator(%) = ( Indicator_current_year – Indicator_last_year) / (Indicator_current_year) * 100

 

My question is, any clues about the solution, to have this kind of Macro?

Thanks,

 

Sample of data:

DateVar1Var2
1/4/20181052.3611316.885
1/5/20181213.2191733.21
1/6/2018978.20411422.348
1/7/2018680.1459945.5225
1/8/2018456.6003617.2773
1/9/2018150.2401194.8029
1/10/2018677.037860.5974
1/11/20181469.711948.627
1/12/20181179.1921552.934
1/13/20181298.2491669.028
1/14/20181358.4771603.955
1/15/20181221.7061335.553
1/16/20181454.2111362.172
1/17/20181721.991774.3
1/18/20181689.8632157.368
1/19/20181040.451537.941
1/20/2018859.51391232.655
1/21/20181426.1322046.319
1/22/2018938.50961365.816
1/23/20181457.2512025.362
1/24/2018881.55621198.344
1/25/20181480.5521961.99
1/26/20181926.7712479.684
1/27/20181916.552461.287
1/28/20181673.0232225.212
1/29/20181765.8742462.233
1/30/20181789.7012533.19
1/31/20181676.4452443.382
2/1/20181800.1342418.202
2/2/20181921.7472369.32
2/3/20182066.4642468.181
2/4/20181959.3512477.953
2/5/20181807.2812434.835
2/6/20181601.572421.839
2/7/20181510.6552424.343
2/8/20181605.0632420.206
2/9/20181771.4672414.867
2/10/20181929.1822409.557
2/11/20181985.0562441.965
2/12/20181907.4332476.812
2/13/20181795.8332494.92
2/14/20181858.4742568.475
2/15/20181732.2172672.946
2/16/20181729.4252631.901
2/17/20181787.8012611.558
2/18/20182054.6582603.725
2/19/20182076.0142589.475
2/20/20181895.952561.175
2/21/20181718.7482509.298
2/22/20181751.4952441.274
2/23/20181842.3722366.673
2/24/20181805.4442132.074
2/25/20181505.6421904.716
2/26/20181396.8471469.527
2/27/20181421.3161704.773
2/28/20181714.2332032.702
3/1/20181832.0732150.518
3/2/20181932.3192167.833
3/3/20181715.7861861.284
3/4/20181458.2511596.16
3/5/20181339.7391611.28
3/6/20181410.9551642.457
3/7/20181413.3191791.306
3/8/20181509.371801.486
3/9/20181459.7951757.712
3/10/20181472.1631725.644
3/11/20181659.8591656.797
3/12/20182004.2911639.147
3/13/20181956.6141617.089
3/14/20181694.6471580.816
3/15/20181606.1381611.78
3/16/20181603.2941603.493
3/17/20181514.1161576.456
3/18/20181368.4271526.759
3/19/20181196.5181511.448
3/20/20181215.7011513.986
3/21/20181106.1081360.411
3/22/20181363.9921575.326
3/23/20181337.3061697.756
3/24/20181260.2761617.235
3/25/20181381.3191691.405
3/26/20181483.1691704.757
3/27/20181314.0111637.299
3/28/20181477.241872.673
3/29/20181597.641957.048
3/30/20181674.1411972.857
3/31/20181383.661674.452
4/1/20181379.3851770.248
4/2/20181569.7612275.148
4/3/20181474.4172171.795
4/4/20181376.2282210.642
4/5/2018949.98281510.065
4/6/20181246.7851886.985
4/7/20181138.8341793.173
4/8/20181383.1812248.09
4/9/20181365.7972199.225
4/10/20181546.3952403.06
4/11/20181383.6922048.077
4/12/20181680.0162285.013
4/13/20181775.6222572.279
4/14/20181577.2642345.104
4/15/20181423.982041.661
4/16/20181780.3632149.781
4/17/20181466.31697.889
4/18/201880.1146111.1365
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Your data only has one year. Assuming this is just a part of your data, here is some UNTESTED CODE

 

proc summary nway data=have;
    class date;
    format date year.;
    var var1 var2;
    output out=means mean=;
run;
data want;
    set means;
    indicator_year=var1/var2;
    perf_indicator1=100*(indicator_year-lag(indicator_year))/indicator_year;
run;

The idea that you put forth that you need a macro to do this indicates you haven't learned basic DATA step manipulation and basic PROC SUMMARY/PROC MEANS. Learn these things. Learn these things before you even think about macros. SAS provides a huge amount of tools for computing basic statistics, such as a mean, and things like subtraction and division can be done in a DATA step. Learn these things before you try to write a macro. Do not use a macro here, it is simply unnecessary.

 

If you want tested code, provide data as DATA step code (instructions), and make sure that data has at least 2 years. Most of us will not work with "spreadsheet" data that you have provided.

--
Paige Miller

View solution in original post

5 REPLIES 5
JackHamilton
Lapis Lazuli | Level 10

Before writing a macro, you should figure out how to do it in open code.  That doesn't look like a problem where a macro will get you much more than parameterization.

 

If the solution doesn't have to be blazingly fast, you can do it in SQL.  Search for "moving average in sql" in Google.  Some of the solutions you'll find use features that aren't available in SAS's dialect of SQL.

 

Also, make sure you know what you mean by "year".  365 days ago?  365 days ago except in leap years when it's 366 days ago?  Same day of the same month in the previous year?  Same day of the same month in the previous year, but force it to a weekday?  Or to a working day?

 

 

PaigeMiller
Diamond | Level 26

Your data only has one year. Assuming this is just a part of your data, here is some UNTESTED CODE

 

proc summary nway data=have;
    class date;
    format date year.;
    var var1 var2;
    output out=means mean=;
run;
data want;
    set means;
    indicator_year=var1/var2;
    perf_indicator1=100*(indicator_year-lag(indicator_year))/indicator_year;
run;

The idea that you put forth that you need a macro to do this indicates you haven't learned basic DATA step manipulation and basic PROC SUMMARY/PROC MEANS. Learn these things. Learn these things before you even think about macros. SAS provides a huge amount of tools for computing basic statistics, such as a mean, and things like subtraction and division can be done in a DATA step. Learn these things before you try to write a macro. Do not use a macro here, it is simply unnecessary.

 

If you want tested code, provide data as DATA step code (instructions), and make sure that data has at least 2 years. Most of us will not work with "spreadsheet" data that you have provided.

--
Paige Miller
naeemomer
Calcite | Level 5

Morning,

   There seems to be an issue, either with my data-set (and Date format) or with the code you shared. 

 

My data looks like the following (here is just a snapshot - the dataset contains 4 years of data points)

naeemomer_0-1643007170508.png

 

Using the code provided, I get following, which is definitely not what I am looking for. I am looking for yearly averages, for the two variables (which one can calculate in python using "df.resample('Y').sum()" ).

naeemomer_1-1643007574241.png

 I used the following code and got the result illustrated (in attached figure). I shall appreciate any clues/hints to improve the solution in SAS Code

  

proc summary nway data=Sample_table;
	class Date;
	format Date year.;
	var AB2_Water_MBD AB2_PWR_MW;
	output out=means mean=;
title 'Yearly Average of the Water and Power';
run;

proc print data=means;
run;

naeemomer_4-1643007818915.png

 

Thanks and regards,

Omar

Reeza
Super User

That is because you have a datetime (05Mar2019:15:00:00), not a date (05Mar2019) variable so you have the wrong format applied. 

(Your first post shows a date format not a datetime).

 

Try using DTYEAR. instead.

 

proc summary nway data=Sample_table;
	class Date;
	format Date dtyear.;
	var AB2_Water_MBD AB2_PWR_MW;
	output out=means mean=;
title 'Yearly Average of the Water and Power';
run;

proc print data=means;
run;

Not 100% sure if there is a DTYEAR format. 


@naeemomer wrote:

Morning,

   There seems to be an issue, either with my data-set (and Date format) or with the code you shared. 

 

My data looks like the following (here is just a snapshot - the dataset contains 4 years of data points)

naeemomer_0-1643007170508.png

 

Using the code provided, I get following, which is definitely not what I am looking for. I am looking for yearly averages, for the two variables (which one can calculate in python using "df.resample('Y').sum()" ).

naeemomer_1-1643007574241.png

 I used the following code and got the result illustrated (in attached figure). I shall appreciate any clues/hints to improve the solution in SAS Code

  

proc summary nway data=Sample_table;
	class Date;
	format Date year.;
	var AB2_Water_MBD AB2_PWR_MW;
	output out=means mean=;
title 'Yearly Average of the Water and Power';
run;

proc print data=means;
run;

naeemomer_4-1643007818915.png

 

Thanks and regards,

Omar


 

Reeza
Super User
If the data is updated daily, is it a YTD comparison or are you comparing a partial year to a full year? We have reports that include all three for example, a comparison of current quarter to same quarter previous year, YTD compared to YTD last year (ie Up to Jan 16 2021 compared to Up to Jan 16 2022) or YTD compared to full prior year (limited value until later in the year).

Just a note that if you do this and your business has cycles or seasonality it may make sense to align your start dates somehow which is also something we do as we have biweekly payment cycles. If we start one year with one week being a payment week and in the prior week it wasn't it makes it seem like we're always off.

Sorry if you know all of this but it wasn't clear from the post :).

SAS PROC MEANS can easily do all of these calculations by the way.

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!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 5 replies
  • 1294 views
  • 2 likes
  • 4 in conversation