SAS Enterprise Guide - Date value syntax error - sas

Hope someone can help me in my case when in SAS EG.
t1.MIN_of_DATE_PARAM - in date format
t1.MAX_of_DATE_PARAM - in date format
CASE WHEN t1.MIN_of_DATE_PARAM < 01JAN2019 AND t1.MAX_of_DATE_PARAM < 01JAN2019
THEN 'OLD CLIENT' END
how can i compare it to jan12019 which I have declared in the calculation? here's the error i get:
(CASE WHEN t1.MIN_of_DATE_PARAM < 01JAN2019 AND t1.MAX_of_DATE_PARAM < 01JAN2019
THEN 'OLD CLIENT' END)
_________
22
76
ERROR 22-322: Syntax error, expecting one of the following:
enter image description here

The error is underlined in the log, you can't specify a date value in SAS simply by putting 01JAN2019, you need to do the following:
CASE WHEN t1.MIN_of_DATE_PARAM < '01JAN2019'd AND t1.MAX_of_DATE_PARAM < '01JAN2019'd
THEN 'OLD CLIENT' END
Wrapping the values in ''d converts the provided date-string into a SAS date value which can be compared with the date variables. For more information, search the SAS documentation for "SAS Date Constants".

Related

Why return string format for FINFO function in SAS EG and SAS DI is different

I am trying to get file "modeified" datetime with
datetimeString = finfo(fid,'Last Modified');``
In SAS EG the return string looks like 12Jan2023:11:03:53
But in SAS DI the return string looks like 12 January 2023 11:03:28
I am trying to convert a string to a datetime like below and obviously it doesn't work for EG and throw invalid argument error.
moddate=input(finfo(fid,'Last Modified'),datetime20.);
I can fix this by writing bit of extra code in DI but would like to know why finfo(fid,'Last Modified'); return different string format?
I am working in data step.
Check the setting of the LOCALE option in the two different SAS sessions.
You can use the NLDATM informat to read the string generated by FINFO()
SAS Documentation on NLDATM informat
Example program that uses NLDATM informat to get datetime from FINFO() function.
Use a different in-format
37 data _null_;
38 x = '12 January 2023 11:03:28';
39 moddate=input(x,datetime20.);
40 moddate2=input(x,anydtdtm30.);
41 put 'NOTE: ' (mod:)(=datetime.);
42 run;
NOTE: Invalid argument to function INPUT at line 39 column 12.
NOTE: moddate=. moddate2=12JAN23:11:03:28

SAS date imported wrong

When I imported my excel sheet some dates imported differently than others. I tried to fix this with the code below to format the date.
DATA volume;
SET mice.volume;
format Date MMDDYY10.;
run;
However, I received the following error.
ERROR 48-59: The format $MMDDYY was not found or could not be loaded.
I had also tried with the following code
DATA volume;
SET mice.volume;
If date= 44138 then date= '11/3';
If date= 44141 then date= '11/6';
run;
NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
A Proc Contents shows the variable= Date type= Char Len=7 format=$7 Informat=$7 Label= Date
How do I fix this?
The date column being character having a mix of 'date looking' strings, and Excel date value numbers tells me some of the date values in your Excel are actually strings, such as '11/10 or ='11/10'.
The raw number 44138 is:
a SAS date value is 04-NOV-2080 (obviously not what is wanted)
an Excel date value 03-NOV-2020 (aha!)
03-NOV-2020 as SAS date value is 22222
an offset of -21916 from Excel
-21916 is the SAS date 30-DEC-1899
Date Epochs
An epoch is the date corresponding to a base number 0 in a systems calendar. SAS Base year is 1960 and Excel Base year is 1900.
Formatted
Number Actual Date Date Shown System/Format
------ ----------- ----------- ----------
0 31-DEC-1899 1/0/1900 Excel / Short Date (Formatter is weird at Epoch)
0 01-JAN-1960 01-JAN-1960 SAS / DATE11.
21916 01-JAN-1960 1/1/1960 Excel / Short Date
-21916 30-DEC-1899 31-DEC-1899 SAS / DATE11.
Notice the round trip is from 31-dec-1899 to 30-dec-1899. This due to an Excel 97 bug that has been carried forth for legacy reasons. See Microsoft's explanation in "Excel incorrectly assumes that the year 1900 is a leap year" which pushes the blame back even further to Lotus 1-2-3
The formula to convert between systems S1 and S2 date numbers is to add the # for the other systems epoch date (# # 0)
SAS#(date) = Excel#(date) + SAS#(Excel Epoch Date) - 1 (Excel leap year bug), or
sas_dt = excel_dt + '31-DEC-1899'd - 1; *or;
sas_dt = excel_dt + '30-DEC-1899'd;
What happened
Mixed value types in the Excel date column forced IMPORT to perceive the date variable as character.
The Excel cells with a date looking m/d string were brought in as the string
The Excel cells with a date, likely custom formatted as m/d, were brought in as the underlying Excel date number.
The ERROR
You tried to apply the date format MMDDYY. to the character variable Date.
A character column can not be assigned a numeric or date format, thus you get the
ERROR 48-59: The format $MMDDYY was not found or could not be loaded.
SAS automatically presumed MMDDYY. meant a character format $MMDDYY. because the variable type was character.
The Fix
You can convert the values in the character date column with code such as the following (untested):
if index(date,'/') then
date_fixed = input (trim(date)||'/2020', mmddyy10.);
else
date_fixed = input(date,best12.) + '30-DEC-1899'D;
format date_fixed yymmdd10.;
If you want to continue showing only mm/dd in SAS, use the format NLDATEM5.
format date_fixed NLDATEM5.;

month name not working by using format function in power bi

I have used the following to get the stating day of the month
multiemp[Day] - WEEKDAY(multiemp[Day],2)
For example if my date us 22 May 2018, after using the above query, got the expected out put.i.e., 20 May 2018
Now I tried to get the month name by using the above query and format function
format(month(multiemp[Day] - WEEKDAY(multiemp[Day],2)),"mmm")
results were not expected, instead of may January is getting populated.
when verify with the following month function expected results arrived .i.e, 5
month(multiemp[Day] - WEEKDAY(multiemp[Day],2)
but only issue in changing the month number to month name
Please find below:
formulas used
weekstartday = multiemp[Day] - WEEKDAY(multiemp[Day],2)
weekstartday_month = month(multiemp[weekstartday])
Month_name = format(multiemp[weekstartday_month],"mmm")
another_ans = format(dateadd(multiemp[Day],-weekday(multiemp[Day],2),day),"mmm")
another_answer_date = dateadd(multiemp[Day],-weekday(multiemp[Day],2),day)
EDIT: Day 2
Modified the datatype of the column to date time/timezone.
after refresh the data didn't change
Found out the solution
Solution 1:
mnname = format(multiemp[weekstartday].[Date],"mmm")
Solution 2:
Month_name = format(date(YEAR(multiemp[weekstartday]),MONTH(multiemp[weekstartday]),day(multiemp[weekstartday])),"mmm")
Thanks in advance
Format the column instead of repeating the column DAX statement.
Column = FORMAT(*nameOfYourStartOfWeekColumn*,"mmm")
Because FORMAT(...,"MMM") or FORMAT(...,"MMMM") takes as an argument a date types and non numeric types, try this
format(dateadd(multiemp[Day],-weekday(multiemp[Day],2),day),"MMM")
If you want the starting day of the week as monday, then,
format(dateadd(multiemp[Day],-weekday(multiemp[Day],3),day),"MMM")
EDIT
Verify that date column is date or date\time type.
Solution 1:
mnname = format(multiemp[weekstartday].[Date],"mmm")
Solution 2:
Month_name = format(date(YEAR(multiemp[weekstartday]),MONTH(multiemp[weekstartday]),day(multiemp[weekstartday])),"mmm")

Defining a new field conditionally using put function with user-defined formats

I am trying to define a new value for an observation with a user defined format. However, my if/then/else statement seems to only work for observations with a year value of "2014". The put statements are not working for other values. In SAS, the put statement is blue in the first statement, and black in the other two. Here is a picture of what I mean:
Does anyone know what I am missing here? Here is my complete code:
data claims_t03_group;
set output.claims_t02_group;
if year = "2014" then test = put(compress(lookup,"_"),$G_14_PROD35.);
else if year = "2015" then test = put(compress(lookup,"_"),$G_15_PROD35.);
else test = put(compress(lookup,"_"),$G_16_PROD35.);
run;
Here is an example of what I mean when I say that the process seems to "work" for 2014:
As you can see, when the Year value is 2014, the format lookup works correctly, and the test field returns the value I am expecting. However, for years 2015 and 2016, the test field returns the lookup value without any formatting.
Your code utilises user-defined formats, $G_14_PROD.-$G_16_PROD.. My guess would be that there is a problem with one or more of these, but unless you can provide the format definitions it will be difficult to assist you further.
Try running the following and sharing the resulting output dataset work.prdfmts:
proc sql noprint;
select cats(libname,'.',memname) into :myfmtlib
from sashelp.vcatalg
where objname = 'G_14_PROD';
quit;
proc format cntlout = prdfmts library=&myfmtlib;
select G_14_PROD G_15_PROD G_16_PROD;
run;
N.B. this assumes that you only have one catalogue containing a format with that name, and that the format definitions for all 3 formats are contained in the same catalogue. If not, you will need to adapt this a bit and run it once for each format to find and export the definition.
Not that it solves your actual problem, but you could eliminate the IF/THEN by using the PUTC() function instead.
data have ;
do year=2014,2015,2016;
do lookup='00_01','00_02' ;
output;
end;
end;
run;
proc format ;
value $G_14_PROD '0001'='2014 - 1' '0002'='2014 - 2' ;
value $G_15_PROD '0001'='2015 - 1' '0002'='2015 - 2' ;
value $G_16_PROD '0001'='2016 - 1' '0002'='2016 - 2' ;
run;
data want ;
set have ;
length test $35 ;
if 2014 <= year <= 2016 then
test = putc(compress(lookup,'_'),cats('$G_',year-2000,'_PROD.'))
;
run;
Result
Obs year lookup test
1 2014 00_01 2014 - 1
2 2014 00_02 2014 - 2
3 2015 00_01 2015 - 1
4 2015 00_02 2015 - 2
5 2016 00_01 2016 - 1
6 2016 00_02 2016 - 2

Sharepoint 2010 Task list: Due date = Start date + 1 month

I want to auto-fill the Due date column by taking the Start date column and add 1 month to it.
This formula I've tried in different ways:
=DATE([Start Date]+[Gap],"dd-mm-yyyy") (I created Gap column as number type and filled it with 28 days; in our language, dd-mm-yyyy seems to be the usual way of showing dates in Sharepoint).
Sharepoint keeps on giving back errors.
Try this formula:
=DATE(YEAR([Start Date]);MONTH([Start Date])+[Gap];DAY([Start Date]))
where [Gap] value is integer.
Note: pay attetion to delimiters - they are locale-dependent, most probably you need to replace , with ;