SAS aggregation seconds into minutes - sas

I have data structured where for every second (YY-MM-DD-HH-MM-SS) I have actual car's speed. I want to aggregate my seconds into minutes (or 5, 10, 15 minutes) and have the average speed for that last minute (or 5,10,...).

Use proc timeseries, then choose your interval. For example, if you want to change it to 15 minute averages try
proc timeseries data = rawData output= minuteAvg;
id timestamp interval = Minute15 accumulate = avg;
run;
I've guessed the names of your time variable as you haven't supplied it. You can change the interval by suffixing the number of minutes to Minute (Minute1, Minute5, Minute10, etc.) or another interval keyword (Hour, Hour2, Hour12, Day, etc.).

Related

Subtracting current hour's value from previous hour's value in Power BI

Is there a DATEADD equivalent for hours?
The smallest interval for DATEADD is DAY, but I need it to be HOUR.
I have a running total number that grows once per hour, I need to make a measure that calculates the hourly consumption. The total dataset spans over many years, with 24 readings per day, every day.
For example for monthly calculations (on a different solution) I use
Monthly = SUM('Readings'[VALUE]) - CALCULATE(SUM('Readings'[VALUE]), DATEADD('Date'[Date], -1, MONTH))
I need a similar solution to calculate hourly consumption for each of the 24 hours.
Sample table 'Readings':
View Table
So I would need a measure that knows for example on 01/01/20 at hour 1 (01:00) the consumption was 5 (145-140) etc.

Push Dataset in powerBI

I have a requirement where data from Event Hub is continuously coming to Stream Analytics and I am pushing it to POWERBI with a tumbling window of 15 mins.The Push data set will have data for a week.
The data is Transaction data where I have Amount and Transaction Date
For my requirement i need 3 charts
total Amount for today till current time ..SO suppose 04th Nov (6:41 pm) it is $100
change in Amount (Total Amount last week same day till same time- total Amount for today till current time) --Suppose last week it is 28 Oct till 6:41 pm it was #$80 so it should show #$20
KPI where it find percentage difference between first two. 20*100/100=20%
as I am using the tumbling window of 15 mins so i wont have transaction exactly till 6:41 but at least Amount where Max(Transaction Date)<= CurrentTime (6:41) for that day .
I am not sure how can I achieve 2nd and 3rd?
today = CALCULATE(SUM('Table'[amount]),FILTER(ALL('Table'),'Table'[date]=TODAY()&&'Table'[datetime]<=NOW()))
total_last week = var lastweek=TODAY()-7 return CALCULATE(SUM('Table'[amount]),FILTER(ALL('Table'),'Table'[date]=lastweek&&'Table'[datetime]<=NOW()-7))

Use a slicer on a calculated column

I need to find one way or another the following formula in Power BI:
Total Hours of Use of a Machine = Hours Function * Range of Functioning
where Hours Function is the hours of use of a certain machine. Take it at a cost that for each machine is a constant and Range of Functioning is the difference between the final date of the evaluation and the initial date, measured in hours.
For example, I want to measure the Total Hour Use of a Machine in between 15/10/2019 and 14/20/2019. So the math is the following:
Assume: 2 machines
Hours Function machine A: 6
Hours Function machine B: 9
Range of Functioning = 15/10/2019 - 14/10/2019 = 24 hours
The output:
Total Hours of Use of a Machine A: 144
Total Hours of Use of a Machine B: 216
I need to do that in Power BI in a way that any user moving a slicer of date, refresh the Total Hours of Use of a Machine.
I don't find any way that I can get the difference between the final date of the evaluation and the initial date and put in DAX or a new column.
You have to use measures if you want to recalculate the value when you change the date with a slicer.
The first step is to be sure to be able to calculate the number of day selected by your slicer.
It seems to be easy but if you use the function FirstDate on your calendar table directly integrated in PowerBI.
You'll never have what you expect.
The tricks here to get this number of day is to calculate the number of rows in your calendar table with the function countrows.
When you have this number day you just have to multiply this by 24 ( hours) and by the sum of your "Hours Function machine".( 6 for A 9 for B in your example )
( It's important to use the sum or another aggregate function like average because if you have multiple value the measure fall in error because it need only one value to multiply).
The dax formula looks like :
= COUNTROWS(('Calendar')) * Sum(Machine[Hours function])
You can then display this measure filtered by the Machine Name and a date slicer(based on your calendar table).

Getting the average for multiple samples for variables in SAS

I have a time-series data set with 5 minute resolution for 4 years in SAS. In this data set,a particular several variables (e.g., SRAD) is measured for each 5 minutes at 9 different stations (e.g., ST1,..., ST9). It means for each 5 minute interval (for example, Feb. 1, 2015 1:00:00 PM) there are 9 samples for this variable. Here is are some of my variables:
Interval TAIR SRAD Date Time Month Day Year Hour Minute
Now, I am wondoering how I can create a new dataset including the data of Feb 1 to Feb 10, 2015 from 13-19 (time) with average of each variable from all stations. In other words, for each 5 min interval I need the average of all variables instead of 9 variables associated with the 9 stations (ST). Like this:
Interval TAIR_ave SRAD_ave Date Time Month Day Year Hour Minute
I need to do it in SAS. It'd be really appreciated if you can help.
Best Regards,
Paalang

How can I group intra-day time periods into groups of 15 minutes in SAS more efficiently?

I am trying to divide the trading hours of 9:30 am - 4:00 pm into 15 minute intervals. I want to do an analysis for each 15 minute interval separately. How can I simplify the below code as opposed to repeating that if statement for 26 intraday 15 minute intervals?
I need to have a variable named interval as below because then I can choose to run my analysis only on certain intervals such as "if interval<3".
Thanks.
int=(minute+(hour-9)*60)-29;
if int<16 then interval=1;
if int=>16 and int< 31 then interval=2;
if int=>31 and int< 46 then interval=3;
if int=>46 and int< 61 then interval=4;
A more SASsy way is to use INTCK.
*Create test dataset;
data have;
format trade_time TIME9.;
do trade_time = '09:00:00't to '16:00:00't by 480;
output;
end;
run;
data want;
set have;
interval = intck('MINUTE15','09:00:00't,trade_time)+1;
run;
This is a bit simpler math-wise and more flexible in that it can easily calculate any kind of interval.
intck takes three arguments in its simplest form: the kind of interval, the starting time, and the ending time, and tells you how many times you crossed an interval boundary to get to that ending time from the starting time. The kind of interval is a combination of the kind of time unit ('MINUTE','MONTH','HOUR', whatnot) and a pair of numbers separated by a decimal if required; left hand is the number of the interval, right hand is the shift (so if you want to start your 15 minute interval at the 5 minute mark, it would be MINUTE15.5).
interval=ceil(((minute+(hour-9)*60)-30)/15);