Clarifications on Domo Beast mode - domo

I am trying to extract the date from a field based on a pattern - If the first four characters of the field description are *SLD then I need to extract the date from the field description as output date else I need to take the date from the field orddate as the the output date..please find the table below:
description
orddate
output Date
*SLD 5/18/22 Rimel
5/2/2022
5/18/2022
SOLD mila
5/3/2022
5/3/2022
*SLD 5/23/22 345671 kilo
5/15/2022
5/23/2022
*SLD 5/14/22 jing ming
5/1/2022
5/14/2022
123566
5/12/2022
5/12/2022
The following Beast mode also contains the pattern SLD followed by the date with no space..
DATE(
CASE WHEN LEFT(`description`,5) = '*SLD ' THEN
IFNULL(TRY_CAST(SPLIT_PART(`description`,' ',2) AS DATE),`orddate`)
WHEN LEFT(`description`,4) = '*SLD' THEN
IFNULL(TRY_CAST(SPLIT_PART(REPLACE(`description`,'*SLD',''),' ',1) AS DATE),`orddate`)
ELSE
`orddate`
END)
Error:ERROR 3429: For 'isnull', types date and varchar are inconsistent

Related

Beast mode to extract the date based on a pattern

I am trying to extract the date from a field based on a pattern - If the first four characters of the field description are *SLD then I need to extract the date from the field description as output date else I need to take the date from the field orddate as the the output date..please find the table below:
description
orddate
output Date
*SLD 5/18/22 Rimel
5/2/2022
5/18/2022
SOLD mila
5/3/2022
5/3/2022
*SLD 5/23/22 345671 kilo
5/15/2022
5/23/2022
*SLD 5/14/22 jing ming
5/1/2022
5/14/2022
123566
5/12/2022
5/12/2022
I would do something like:
Case
When Left(description,4) = '*SLD' Then
Replace( /*as months can have 1 or 2 digits, the resulting string could have some spaces that we need to clean*/
Left( /* Remove characters after the date */
Right(description, Len(description) - 5) /* Remove '*SLD ' */
,8
)
,' ',''
)
Else orddate
End

How to convert date field to this format 2018 / 01

I have one date field in my table as char "01jan2017",
I want to convert the date field to this format "2018 / 01" and there should be space between forward slash.
Thanks
If what you're looking for is just for display then here is a character conversion
data r;
date = '01jan2017'd;
date1 = compbl(put(year(date),best.)|| " / "||put(month(date),z2.));
run;
There are three key steps you need to do:
catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
Convert the date to date9. format in order to extract the year and month,
Use the z2. format for the month to get the leading Zero,
Use Catx() to concatinate the year , month & ' / '.
Full Code:
data want;
date_char="01jan2017";
dateYYMM=catx(' / ',year(input(date_char,date9.)),put(month(input(date_char,date9.)),z2.));
run;
Output:
date_char=01jan2017 dateYYMM=2017 / 01

SAS check if year is IN character

I'm new to SAS and trying to do the next:
I have two tables (users & reviews) connected through user_id.
I've merged both via user_id.
There are two variables I want to use:
"date" (of the review. Saved as format MMDDYY10.)
"elite" (year a user was elite, saved as a character)
Elite has different forms (examples):
empty cell
2012,2015,2017
2012:2017
How do I check if a user was elite when he wrote the review?
Thanks in advance,
Peyo
This covers the case when a year is not present in the text of elite but is included in a range:
data test_cases;
input date yymmdd10. + 1 elite $32.;
format date yymmdd10.;
infile cards missover;
cards;
2012-01-01 2012,2015,2017
2012-01-01 2012:2017
2013-01-01 2012:2017
2012-01-01
2011-01-01 2012,2015,2017
2011-01-01 2012:2017
;
run;
data example;
set test_cases;
article_year = put(year(date),4.);
if index(elite,':') = 0 then elite_flag = index(elite,article_year);
else elite_flag = substr(elite,1,4) <= article_year <= substr(elite,6,9);
run;
You can use the YEAR function to extract the year from date, then use the PUT function to convert it to character and finally the INDEX function to search the elite list:
if index(elite,put(year(date),4.)) then ...
The condition will be true when the index function founds the year of date within the elite variable.

teradata yymmdd date format

I have a field that should be 6 digit character but it is numeric. I am using the following code to add the leading zero:
select CAST(CAST(CHD_OPEN_DATE AS FORMAT '9(6)') AS CHAR(9))
I'm using the following code to format this as a date:
cast(cast(lpad(to_char(CHD_OPEN_DATE),6,'0') as date format 'YYMMDD') as date format 'YYYY-MM-DD')
When using this date format 1990 comes up as 2090. Is there a work-around for this?
If your number has a YYMMDD format you can use the following to cast to a date without the need to cast to an intermediate string. Assuming a date range between 1930 and 2029:
SELECT 900331 AS CHD_OPEN_DATE,
Cast(CASE WHEN CHD_OPEN_DATE < 300000
THEN CHD_OPEN_DATE + 1000000
ELSE CHD_OPEN_DATE
END AS DATE)

Problem when extracting year and week number from string in PSQL

Let's say that I have a range of SQL tables that are named name_YYYY_WW where YYYY = year and WW = week number. If I call upon a function that guides a user defined date to the right table.
If the date entered is "20110101":
SELECT EXTRACT (WEEK FROM DATE '20110101') returns 52 and
SELECT EXTRACT (YEAR FROM DATE '20110101') returns 2011.
While is nothing wrong with these results I want "20110101" to either point to table name_2010_52 or name_2011_01, not name_2011_52 as it does now when I concanate the results to form the query for the table.
Any elegant solutions to this problem?
The function to_char() will allow you to format a date or timestamp to output correct the iso week and iso year.
SELECT to_char('2011-01-01'::date, 'IYYY_IW') as iso_year_week;
will produce:
iso_year_week
---------------
2010_52
(1 row)
You could use a CASE:
WITH sub(field) AS (
SELECT CAST('20110101' AS date) -- just to test
)
SELECT
CASE
WHEN EXTRACT (WEEK FROM field ) > 1 AND EXTRACT (MONTH FROM field) = 1 AND EXTRACT (DAY FROM field) < 3 THEN 1
ELSE
EXTRACT (WEEK FROM field)
END
FROM
sub;