Proc sql sum for each month - sas

I have a table with dates and prices:
28/01/2016 100
04/02/2016 200
16/04/2016 100
23/04/2016 150
07/05/2016 150
14/05/2016 200
.
.
01/12/2017 80
08/12/2017 200
I would like to get a sum based on the month and then year, as follows:
01/2016 100
02/2016 200
04/2016 250
05/2016 350
.
.
12/2017 280
The date is currently in DDMMYY10. format.

Convert any individual date value within a month to a the same value and group by that. For example by using INTNX() function.
select intnx('month',date,'b') as month format=yymm7.
, sum(price) as total_price
from have
group by month
Or convert it to a string using PUT() function.
select put(date,yymm7.) as month
, sum(price) as total_price
from have
group by month

Related

Week Ending Date from Week no. - GCP

How do I get the week ending date from week no. in GCP ?
SELECT 
Session,
Store_Num,
Store_Name,
EXTRACT (WEEK(Monday) from Route_Date) as Weekno,
AVG(Planned_DPH) as Avg
FROM `TABLE` 
WHERE Route_Date  between current_date() - 60 and current_date()
and Store_Num = 8881
GROUP BY 1,2,3,4
ORDER BY Weekno
You can get the first day of weekno with PARSE_DATE function and %W format string.
%W - The week number of the year (Monday as the first day of the week) as a decimal number (00-53).
And by adding 6 more days to the first day of the week, you will get the week ending date.
Consider below sample query.
WITH sample_data AS (
SELECT DATE '2022-11-10' AS Route_Date
)
SELECT *,
PARSE_DATE('%Y%W', Year || Weekno) AS startday_of_weekno,
PARSE_DATE('%Y%W', Year || Weekno) + 6 AS lastday_of_weekno
FROM (
SELECT EXTRACT(WEEK(Monday) from Route_Date) as Weekno, -- 45
EXTRACT(YEAR FROM Route_Date) AS Year,
FROM sample_data
);
Query results

Dax calculate a metric for another day in the calendar table

I have a table with a date column and a money column:
Thedate CashCollected
2039-12-28 100
2039-12-27 200
2039-12-26 300
2039-12-25 400
2039-12-24 500
2039-12-23 600
2039-12-22 700
2039-12-21 800
and then I have the date table with the date and the previous working day calculated following some specific conditions:
TheDate PrevItWorkDay
2039-12-28 2039-12-27
2039-12-27 2039-12-23
2039-12-26 2039-12-23
2039-12-25 2039-12-23
2039-12-24 2039-12-23
2039-12-23 2039-12-22
2039-12-22 2039-12-21
2039-12-21 2039-12-20
I would like to calculate in DAX a measure which gives me the sum of cash collected for the previous working day in the date table:
TheDate PrevItWorkDay
2039-12-28 200
2039-12-27 600
2039-12-26 600
2039-12-25 600
2039-12-24 600
2039-12-23 700
2039-12-22 800
2039-12-21
How can I do this?
PrevWorkingDaySum =
CALCULATE(
SUM(TransactionTable[CashCollected]) ,
TransactionTable[Thedate]= SELECTEDVALUE(DateTable[PrevItWorkDay])
)

BigQuery convert Month Year column to timestamp or date

I have a table
Year_month amount
08-2021 100
09-2021 200
10-2021 300
I want to add a column Month to be
Year_month Month amount
08-2021 01-08-2021 100
09-2021 01-09-2021 200
10-2021 01-10-2021 300
It depends of the type of data you have. check below code:
with sodata as (
select '08-2021' as Year_month_string, PARSE_DATE("%m-%Y", "08-2021") as Year_month_date, 100 amount,
union all
select '09-2021',PARSE_DATE("%m-%Y", "09-2021"), 200
union all
select '10-2021',PARSE_DATE("%m-%Y", "10-2021"), 300
)
SELECT Year_month_string,FORMAT('01-%s',Year_month_string) as using_format,FORMAT_DATE("%d-%m-%Y", Year_month_date) as using_format_date,amount from sodata
output
Row
Year_month
using_format
using_format_date
amount
1
08-2021
01-08-2021
01-08-2021
100
2
09-2021
01-09-2021
01-09-2021
200
3
10-2021
01-10-2021
01-10-2021
300
To see more details about the functions used please go to this links:
Format (string)
Format Date
You can use Parse date function
SELECT PARSE_DATE("%m-%Y", "08-2021")

Avoid manual column creation in Teradata SQL

Consider the following Teradata View named 'VIEW' which consists of Transactional data.
ATTR1 ATTR2 DATE1 DATE2 WEEK1 WEEK2 AMOUNT
A B 1/1/2019 1/8/2019 201901 201902 10
A B 12/26/2018 1/8/2019 201852 201902 20
A B 1/1/2019 1/15/2019 201901 201903 30
A B 1/8/2019 1/15/2019 201902 201903 30
DATE1 is a Posting Date and DATE2 is the clearing date of the transaction. WEEK1 and WEEK2 are the fiscal weeks of DATE1 and DATE2 respectively. ATTR are random attributes of the transaction. I need to report the transaction amounts by the 'week of' for the attributes.
For example for Week 201901 we would like to see the transactions amounts of posting dates of and before Week 201901 and the clearing dates after 201901. See code below.
select ATTR1,
ATTR2,
SUM(CASE WHEN WEEK2 > 201852 AND WEEK1 <= 201852 THEN AMOUNT END) AS AMT_201852,
SUM(CASE WHEN WEEK2 > 201901 AND WEEK1 <= 201901 THEN AMOUNT END) AS AMT_201901,
SUM(CASE WHEN WEEK2 > 201902 AND WEEK1 <= 201902 THEN AMOUNT END) AS AMT_201902,
FROM VIEW
GROUP BY 1,2
The result:
ATTR1 ATTR2 AMT_201852 AMT_201901 AMT_201902
A B 20 60 60
As the code above suggests, we are having to manually create columns for each week which we would like to avoid. Is there a way to dynamically create these columns as the weeks pass by? Or is there a better way to represent this?
In a report, using WEEK1 as a filter will filter out the earlier weeks (In case of WEEK1 as 201901, 201852 will be filtered out and lose the respective amounts). We eventually put this SQL into a PowerBI dashboard.
Thanks!
You'll have to mess around with this a bit, but it should get you going in the right direction.
Teradata has an extremely useful date view built in: sys_calendar.calendar
This, for example, will get you the past three weeks in the format you have them in your sample data (YYYYWW):
select
distinct week_of_year,
year_of_calendar,
(year_of_calendar * 100) + week_of_year as year_week
from
sys_calendar.calendar
where
calendar_date between (current_date - interval '21' day) and current_date
order by year_week
You should be able to replace current_date with a parameter to make this dynamic, and join this to your table.

In sas application Set date parameter and put it in the contacted column to retrieve data in a certain period

The date in the table is not one set,
Days in the days column and months in the month column and years in the year column
I have concatenated the columns and then put these concatenation in where clause and put the parameter I have made but I got no result
I assume you are querying a date dimension table, and you want to extract the record that matches a certain date.
Solution:
I created a dates table to match with,
data dates;
input key day month year ;
datalines;
1 19 2 2018
2 20 2 2018
3 21 2 2018
4 22 2 2018
;;;
run;
Output:
In the where clause I parse the date '20feb2018'd using day, month & year functions: in SAS you have to quote the dates in [''d]
proc sql;
select * from dates
/*if you want to match todays' date: replace '20feb2018'd with today()*/
where day('20feb2018'd)=day and month('20feb2018'd)=month and year('20feb2018'd)=year;
quit;
Output:
if you compare date from day month and year, then use mdy function in where clause as shown below. it is not totally clear what you are looking for.
proc sql;
select * from dates
where mdy(month,day, year) between '19feb2018'd and '21feb2018'd ;