Executed the below SAS script with case statement to populate the value of subscriber_type as 'Primary' if the date fields CFR_CREATE_DATE = BAN_START_DATE
and if the difference between CFR_CREATE_DATE minus BAN_START_DATE <= 30 days then the value of subscriber_type will be 'AAL <= 30'
and if the difference between CFR_CREATE_DATE minus BAN_START_DATE > 30
then 'AAL > 30' else Null. How can this be done in PROC SQL?
Note: CFR_CREATE_DATE & BAN_START_DATE used in the below script are both date data type fields
SAS SCRIPT:
proc sql;
connect to oracle(&xyz);
create table CANCELS_SUB as
Select
CASE
WHEN TRUNC(b.CFR_CREATE_DATE) = TRUNC(b.BAN_START_DATE) THEN 'PRIMARY'
WHEN TRUNC(b.CFR_CREATE_DATE) <> TRUNC(b.BAN_START_DATE)
AND (TRUNC(b.CFR_CREATE_DATE) - TRUNC(b.BAN_START_DATE)) <= 30 THEN 'AAL <= 30'
WHEN TRUNC(b.CFR_CREATE_DATE) <> TRUNC(b.BAN_START_DATE)
AND (TRUNC(b.CFR_CREATE_DATE) - TRUNC(b.BAN_START_DATE)) > 30 THEN 'AAL > 30'
ELSE NULL
END as SUBSCRIBER_TYPE
from WORK.'COPY OF TRANSACTIONAL SURVEY DAT'n a
left join xys.DAILY_CONSUMER_POST_CANCELS b
ON a.BAN = b.BAN ;
Error Log:
NOTE: The "<>" operator is interpreted as "not equals".
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Expression using subtraction (-) requires numeric types.
2 The SAS System 11:31 Wednesday, July 21, 2021
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Expression using subtraction (-) requires numeric types.
ERROR: The following columns were not found in the contributing tables: NULL.
If you want to compare dates then why are you using "trunc"? That function is designed to truncate a numeric value to a specified number of bytes. Which expects a "number of bytes" as an argument and most of your "dates" would no longer represent the same date if truncated.
If the value returned from Oracle is actually a datetime value then you want to use either the function DATEPART(variable) to get just the date portion of a value that includes time, or use the INTCK function such as
if intck('dtday',b.CFR_CREATE_DATE,b.BAN_START_DATE)=0 then "Primary"
The Intck function returns number of intervals between two date, datetime or time values. The first parameter is the type of interval. If the values are datetime you use DTday, DTmonth, DTweek, DTyear to find the number of intervals based on day, month, week or year. If the values were simple dates then intervals are like day, week, month, year.
If you want to compare dates then why are you using "trunc"? That function is designed to truncate a numeric value to a specified number of bytes. Which expects a "number of bytes" as an argument and most of your "dates" would no longer represent the same date if truncated.
If the value returned from Oracle is actually a datetime value then you want to use either the function DATEPART(variable) to get just the date portion of a value that includes time, or use the INTCK function such as
if intck('dtday',b.CFR_CREATE_DATE,b.BAN_START_DATE)=0 then "Primary"
The Intck function returns number of intervals between two date, datetime or time values. The first parameter is the type of interval. If the values are datetime you use DTday, DTmonth, DTweek, DTyear to find the number of intervals based on day, month, week or year. If the values were simple dates then intervals are like day, week, month, year.
ERROR: Function TRUNC requires at least 2 argument(s).
It requires two arguments, and your calls to TRUNC have only 1 argument. So what are the two arguments? Let's read the documentation at https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/n1lfp83m4yht68n1luxn5tnmuhp3.htm
Honestly, I don't think TRUNC is what you want here, but its not clear to me what other function is appropriate here. Can you state, in words, what you want TRUNC (or a replacement function) to do?
I'm using TRUNC to perform the function of removing the timestamp from the date field to retain only the date portion.
Looks like you're trying to use Pass Through SQL incorrectly so it's using the SAS TRUNC function when you want to use the ORACLE version.
SAS SCRIPT:
proc sql;
connect to oracle(&xyz);
But you're also using a WORK table and a SQL table, which cannot occur in Pass through.
from WORK.'COPY OF TRANSACTIONAL SURVEY DAT'n a
left join xys.DAILY_CONSUMER_POST_CANCELS b
ON a.BAN = b.BAN ;
So you need to not use SQL pass through for sure, ensure you're using SAS functions - so likely DATEPART and not TRUNC.
First figure out the libname/connection statement and then the query below should be pretty close to what you need. Its common for DB to store dates as datetimes, so I think you do need the datepart function here.
libname odbc myDB dsn=myData pwd= user= schema=dbo;
proc sql;
create table CANCELS_SUB as
Select
CASE
WHEN datepart(b.CFR_CREATE_DATE) = datepart(b.BAN_START_DATE) THEN 'PRIMARY'
WHEN datepart(b.CFR_CREATE_DATE) ne datepart(b.BAN_START_DATE)
AND (datepart(b.CFR_CREATE_DATE) - datepart(b.BAN_START_DATE)) <= 30 THEN 'AAL <= 30'
WHEN datepart(b.CFR_CREATE_DATE) ne datepart(b.BAN_START_DATE)
AND (datepart(b.CFR_CREATE_DATE) - datepart(b.BAN_START_DATE)) > 30 THEN 'AAL > 30'
ELSE ''
END as SUBSCRIBER_TYPE
from WORK.'COPY OF TRANSACTIONAL SURVEY DAT'n a
left join xys.DAILY_CONSUMER_POST_CANCELS b
ON a.BAN = b.BAN ;
@vini5 wrote:
Executed the below SAS script with case statement to populate the value of subscriber_type as 'Primary' if the date fields CFR_CREATE_DATE = BAN_START_DATE
and if the difference between CFR_CREATE_DATE minus BAN_START_DATE <= 30 days then the value of subscriber_type will be 'AAL <= 30'
and if the difference between CFR_CREATE_DATE minus BAN_START_DATE > 30
then 'AAL > 30' else Null. How can this be done in PROC SQL?
Note: CFR_CREATE_DATE & BAN_START_DATE used in the below script are both date data type fields
SAS SCRIPT:
proc sql;
connect to oracle(&xyz);
create table CANCELS_SUB as
Select
CASE
WHEN TRUNC(b.CFR_CREATE_DATE) = TRUNC(b.BAN_START_DATE) THEN 'PRIMARY'
WHEN TRUNC(b.CFR_CREATE_DATE) <> TRUNC(b.BAN_START_DATE)
AND (TRUNC(b.CFR_CREATE_DATE) - TRUNC(b.BAN_START_DATE)) <= 30 THEN 'AAL <= 30'
WHEN TRUNC(b.CFR_CREATE_DATE) <> TRUNC(b.BAN_START_DATE)
AND (TRUNC(b.CFR_CREATE_DATE) - TRUNC(b.BAN_START_DATE)) > 30 THEN 'AAL > 30'
ELSE NULL
END as SUBSCRIBER_TYPEfrom WORK.'COPY OF TRANSACTIONAL SURVEY DAT'n a
left join xys.DAILY_CONSUMER_POST_CANCELS b
ON a.BAN = b.BAN ;
Error Log:
NOTE: The "<>" operator is interpreted as "not equals".
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Expression using subtraction (-) requires numeric types.
2 The SAS System 11:31 Wednesday, July 21, 2021ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Function TRUNC requires at least 2 argument(s).
ERROR: Expression using subtraction (-) requires numeric types.
ERROR: The following columns were not found in the contributing tables: NULL.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.