SAS EG casting sting to date getting different results on different machines - or where is SAS locale setting? - sas

Have string "09/07/2019 20:40:01 EDT"
Run command in SAS EG 7.1 connecting to a remote UNIX server
input(substr(event_time, 1, length(event_time)-4),anydtdtm23.) format DateTime23.3 as event_time
One machine comes back correctly as Sep 07, 2019. (dropping the time bit for clarity)
The other machine comes back as July 09, 2019.
Based on what I've read in related questions it seems to be caused by locale setting
What governs SAS locale setting ? Is it my OS ? the Remote UNIX server ? Something in SAS EG settings ?
Thank you

Don't use a guessing procedure unless you must. The ANYDT... series of informats will assume that ambiguous MDY or DMY stings follow the custom of the current language settings of your SAS session. So if SAS thinks you are in England then '09/07/2019' looks like the 9th of July. But if you are in the US then it looks like September 7th.
Instead read the string using either the MMDDYY or DDMMYY informat, depending on what format the strings are actually stored in. If your source actually is using mixed month day ordering styles then write code that uses information from your data to pick the right informat for a particular observation instead of forcing SAS to use your session setting to decide which one to use.

Related

excel date showing different formats in different machines

I am using spreadsheetformatcellvalue to enter the date in the excel like this
<cfset spreadsheetSetCellValue(objxls, "#datecreated#", 2,1,"Date")>
it works, but i noticed something, i have two machines and in one machine date is displaying as: 02-19-2021
and in other excel it is showing 19/02/2021
what i noticed is that it seems to be using the locale specified of the windows. how can i force to use the format of dd/mm/yyyy across all machines irrespective of locale
can i use something in ColdFusion to make it work..
Thanks
You should be able to just format the date.
<cfset spreadsheetSetCellValue(objxls, "#dateFormat(datecreated, 'dd/mm/yyyy')#", 2,1,"Date")>

SAS studio - formatting

I am attempting to format variables in SAS studio which have been truncated due to the name being longer than 32 characters, when I attempt to format the variables in SAS studio it gives the warning 'this variable in uninitialized'. when I run the same code in SAS EG against the same excel document imported, the code works fine and formats the variable. Why would the same code in SAS studio not work?
code:
data test;
set test1;
format 'variable'n best12.;
run;
Compare the value of option VALIDVARNAME in EG vs Studio. Set it in studio to the same as EG.
Two common ways to view the current setting of an option. Proc OPTIONS or function GetOption
proc options option=validvarname;
run;
%put %sysfunc(getoption(validvarname));
The code won’t be the same because you’re using two different applications with different default settings most likely. As someone else indicated, it’s likely the validvarname option that’s the issue. I would recommend setting it to V7 which avoids these issues. With this setting, SAS converts them to valid variable names by default and you can avoid the rename step entirely.
Supposedly the 32 char limit will be lifted in SAS 9.5. No release date has been announced, SAS 9.4 M5 was recently released so I’m not expecting it super soon.

blocking the values after a specific date

I've got the following question.
I'm trying to run a partial least square forecast on a data model I have. Issue is that I need to block certain line in order to have the forecast for a specific time.
What I want would be the following. For June, every line before May 2014 will be blocked (see the screenshot below).
For May , every line before April 2014 will be blocked (see the screenshot below).
I was thinking of using a delete through a proc sql to do so but this solution seems to be very brutal and I wish to keep my table intact.
Question : Is there a way to block the line for a specific date with needing a deletion?
Many thanks for any insight you can give me as I've never done that before and don't know if there is a way to do that (I did not find anything on the net).
Edit : The aim of the blocking will be to use the missing values and to run the forecast on this missing month namely here in June 2014 and in May 2014 for the second example
I'm not sure what proc you are planning to use, but you should be able to do something like the below.
It builds a control data set, based on a distinct set of dates, including a filter value and building a text data set name. This data set is then called from a data null step.
Call execute is a ridiculously powerful function for this sort of looping behaviour that requires you to build strings that it will than pass as if they were code. Note that column names in the control set are "outside" the string and concatenated with it using ||. The alternative is probably using quite a lot of macro.
proc sql;
create table control_dates as
select distinct
nuov_date,
put(nuov_date,mon3.)||'_results' as out_name
from [csv_import];
quit;
data _null_;
set control_dates;
call execute(
'data '||out_name||';
set control_dates
(where=(nuov_date<'||nouv_date||'));
run;');
call execute('proc [analysis proc] data='||out_name||';run;');
run;

Export SAS data to SPSS, date and datetime

I have data inside SAS.
I want to store the datafile to SPSS format (*.sav)
I use the following program:
PROC export Data=SASdataToStoreInSPSS
FILE="Path\Filename_%sysfunc(today(),date9.).sav"
dbms=sav replace;
RUN;
This works great. Except when I open the file in SPSS the dates are strangly formatted.
For example:
156405 08:51:00
Should be
3-Jan-2011 08:51
I can manually change the data formats in SPSS. So the values are correct date values, except they are not automatically formatted in a readable format.
I tried to change the format in SAS before saving to DATETIME20. or DATETIME23.3. But this does not help.
I want this to work without having to open SPSS and run a Syntax there.
The SPSS files that SAS spits out have to be directly mailed to other users of the data.
I think this is either a bug with SAS's export, or an issue with SPSS where some default changed. What's happening is that SAS is storing it as a SPSS Date - but with width 16, which is not long enough to hold the complete datetime. I don't think you can use DBDSOPTS with DBMS=SPSS, so I don't know that there is a good workaround short of importing the file into SPSS.
You could do that automatically, though, using the SPSS Production facility; I've written an import script before and asked SAS to run spssprod with the batch file. That's an irritating workaround, but it might be the easiest, unless SAS Tech Support can help you (and certainly try that - they are usually only a few hours' turnaround for initial contact at least).
SAS mentioned it has to do with the SPSS driver they use. Apparently it is not an easy fix so they forwarded the issue to second-line tech support.
The workaround you will need is split the dates in two columns. One with date and one with time.
data SPSS2;
set SPSS;
date = put(datepart(DatumSPSS), date9.);
time = put(timepart(DatumSPSS), time8.);
run;
Or you can tell the end user how to change the format of the date in SPSS.
For an automated approach, try this .NET app. You need SPSS, but SAS is not required to convert a large collection of SAS files automatically.
Manual Process included code samples or Application Download

Converting a string to a date in DB2

I am working with a DB2 database for the first time.
I am trying to work with DB2 dates, but the data is stored as a string in the DB2 database.
I want to convert this date-string into an actual date, preferably dropping off time because I want all transactions between 1 Jan 2011 and 26 Jan 2011.
So essentially, I want this MS SQL statement in DB2 magic...
CONVERT(datetime,SETTLEMENTDATE.VALUE,103)
For background, I have got as far as
CAST(SETTLEMENTDATE.VALUE, DATE)
AND
DATE(SETTLEMENTDATE.VALUE)
But I need the expert knowledge of a DB2 whizzkid!
Thanks
Based on your own answer, I'm guessing that your column has data formatted like this:
'DD/MM/YYYY HH:MI:SS'
The actual separators between Day/Month/Year don't matter, nor does anything that comes after the year.
You don't say what version of DB2 you are using or what platform it's running on, so I'm going to assume that it's on Linux, UNIX or Windows.
Almost any recent version of DB2 for Linux/UNIX/Windows (8.2 or later, possibly even older versions), you can do this using the TRANSLATE function:
select
date(translate('GHIJ-DE-AB',column_with_date,'ABCDEFGHIJ'))
from
yourtable
With this solution it doesn't matter what comes after the date in your column.
In DB2 9.7, you can also use the TO_DATE function (similar to Oracle's TO_DATE):
date(to_date(column_with_date,'DD-MM-YYYY HH:MI:SS'))
This requires your data match the formatting string; it's easier to understand when looking at it, but not as flexible as the TRANSLATE option.
I know its old post but still I want to contribute
Above will not work if you have data format like this 'YYYMMDD'
For example:
Dt
20151104
So I tried following in order to get the desired result.
select cast(Left('20151104', 4)||'-'||substring('20151104',5,2)||'-'||substring('20151104', 7,2) as date) from SYSIBM.SYSDUMMY1;
Additionally,
If you want to run the query from MS SQL linked server to DB2(To display only 100 rows).
SELECT top 100 * from OPENQUERY([Linked_Server_Name],
'select cast(Left(''20151104'', 4)||''-''||substring(''20151104'',5,2)||''-''||substring(''20151104'', 7,2) as date) AS Dt
FROM SYSIBM.SYSDUMMY1')
Result after above query:
Dt
2015-11-04
Hope this helps for others.
In format function your can use timestamp_format function.
Example, if the format is YYYYMMDD you can do it :
select TIMESTAMP_FORMAT(yourcolumnchar, 'YYYYMMDD') as YouTimeStamp
from yourtable
you can then adapt then format with elements format foundable here
Okay, seems like a bit of a hack. I have got it to work using a substring, so that only the part of the string with the date (not the time) gets passed into the DATE function...
DATE(substr(SETTLEMENTDATE.VALUE,7,4)||'-'|| substr(SETTLEMENTDATE.VALUE,4,2)||'-'|| substr(SETTLEMENTDATE.VALUE,1,2))
I will still accept any answers that are better than this one!
You can use:
select VARCHAR_FORMAT(creationdate, 'MM/DD/YYYY') from table name