SAS start and end date from consecutive run - date

I have a dataset of customers buying items in multiple batches of consecutive days over the year e.g. Customer A buys on the 1st of January, the 2nd of January and the 3rd of January, stops, then buys again on the 1st of February, the 2nd of February and the 3rd of February.
I'm looking to capture the first and last date of each consecutive batch for each customer (so the usual MIN / MAX will miss out of batches in between dates).
I've experimented with RETAIN and LAG and I'm getting close but its not quite what I want.
How do I create a query that will display two rows for Customer A? i.e. row 1 showing start date of the 1st of January and end date of the 3rd of January; row 2 showing start date of the 1st of February and end date of the 3rd of February.

You are asking to group the values based on the presence of a gap between the dates. So test for that and create a new group number variable. Then you can use that new grouping variable in your analysis.
data want ;
set have ;
by id date;
dif_days = dif(sales_date);
if first.id then group=1;
else if dif_days > 1 then group+1;
run;
You can adjust the number of days in the last IF statement to adjust how large of a gap you want to allow and still consider the events as part of the same group.

Related

Dynamically compare sum of last n entries to sum of n entries depending of selected timespan

I have booking information containing booking values. Every booking has a createdAt date. What I have been doing so far, is to extract the month of the createdAt date, sum up the bookingValue and then compare it to the sum of the previous month using lag function. I used string input to select the corresponding month e.g. 2022-04. My code will execute and show the sum of bookingValue of the month April 2022 and also compare it to March 2022 using lag function.
What I´d like to do is to dynamically select a date range using a datepicker. Like this I can select a date range e.g. from 10th of May 2022 to 20th of May and get the sum of bookingValue. Now the only problem I have, is that my lag function won´t work anymore. What can I do?

Extract highest date per month from a list of dates

I have a date column which I am trying to query to return only the largest date per month.
What I currently have, albeit very simple, returns 99% of what I am looking for. For example, If I list the column in ascending order the first entry is 2016-10-17 and ranges up to 2017-10-06.
A point to note is that the last day of every month may not be present in the data, so I'm really just looking to pull back whatever is the "largest" date present for any existing month.
The query I'm running at the moment looks like
SELECT MAX(date_col)
FROM schema_name.table_name
WHERE <condition1>
AND <condition2>
GROUP BY EXTRACT (MONTH FROM date_col)
ORDER BY max;
This does actually return most of what I'm looking for - what I'm actually getting back is
"2016-11-30"
"2016-12-30"
"2017-01-31"
"2017-02-28"
"2017-03-31"
"2017-04-28"
"2017-05-31"
"2017-06-30"
"2017-07-31"
"2017-08-31"
"2017-09-29"
"2017-10-06"
which are indeed the maximal values present for every month in the column. However, the result set doesn't seem to include the maximum date value from October 2016 (The first months worth of data in the column). There are multiple values in the column for that month, ranging up to 2016-10-31.
If anyone could point out why the max value for this month isn't being returned, I'd much appreciate it.
You are grouping by month (1 to 12) rather than by month and year. Since 2017-10-06 is greater than any day in October 2016, that's what you get for the "October" group.
You should
GROUP BY date_trunc('month', date_col)

How to return the last day of each month

I am creating a view where i have multiple records showing up for each month.
Example: January is showing 20 records and February is showing 30 records. I only want the last record of EACH MONTH to show up. So i want to see 1 record for each month.
I have already tried the first() and last() table calculation, but it does not filter by month, but by column.
If you just apply first and last on the dataaset then you won't get the correct output, If you need for every month then you need to divide the partition to year and month and then apply the max on the specific partition.
Try this way:
Place the order date in Exact date format and change the property to discrete
Now extract the year and month in separate calculated fields and place in detail.
Year:
year(Date)
Month:
Month(date)
Now create one more calculated field and write below code:
WINDOW_MAX(MAX([Order Date]),FIRST(),LAST())
Try This
1 Calculated Field - DateMonth
Datetrunc('month',Date)
2 Calculative field - LastDate
{ FIXED DateMonth: max(Date)}
3 Calculative Field- Filter
if lastDate=Date then 'Yes' else 'No' end

Grails Model Count Number of occurence group by Date month, hour

I have a Grails Domain that contain two date feild
Date updated
Date created
I want to count number of rows based on updated time with respect to month or hour or year. How can I do that. The methods showed on Internet does not work. I am using grails version 2.4.4.
Update:
Example
I have a domain that contain four feids
Date Created
Date updated
Long id
Long idx
I want to find how many idx is there in every group when i group by hour. Like there can be 5 in 12h and 6 in 13th hour... so on.. How can i find those.... My comaparison is not fixed. It can be changed to mont or year... In year it will show the number of idx in each year. in month it will show the number of idx in each month..
How can I do this with grails...
You can use where detached query:
DomainClass.where { year( updated ) == 2014 }.count()
Similarly, hour(), minute() etc.
Refer WHERE queries for details.

Crystal report formula syntax

I have a report I am trying to modify in Crystal. It has a data field that has a formula in it, but I want to use another formula.
This is an example of what I am trying to do.
[((# Days in January) – 15) x (Market Rent/(# Days in January))]
+ [((# Days in February) – 0) x (Market Rent/(# Days in February))]
+ [14 x (Market Rent/(# Days in March))]
I have ADO commands built out for the market rent, and a start date and end date. The months in my example are just that an example. I am not sure how to take my ADO command dates that are entered in on a filter page, and put them in a formula like the one above. Any ideas?
Also, in the first and last para. the -15 and the 14 are for a date in the middle of the month. So if the start date was on the 15th of Jan, and the End date was the 15th of march. This formula would calculate my loss of rent during vacancy.
If I'm reading your question correctly, you want to take a date field and find out how to measure the number of days in that month, the month before it, and the month after it. Here's some Crystal formulas to help you out. Let's assume your date field is called {#DateFld}:
To find the number of days in a particular month relative to a particular date, try this:
local datevar X:=cdate(dateadd("m",0,{#DateFld}));
datediff(
"d",
date(year(X),month(X),1),
date(year(X),month(X)+1,1)
)
I recommend you copy & paste this in 3 different formulas:
- In the 1st formula, replace the "0" with a -1 to get the number of days in the previous month.
- In the 2nd formula, don't change anything. That'll get you the number of days in the current month (i.e. the month that {#DateFld} is in)
- In the 3rd formula, replace the "0" with a +1 to get the number of days in the next month.
For example, if {#DateFld} is March 10th, 2011, the 1st formula will give you 28, the 2nd will give you 31, and the 3rd will give you 30.