create categorical variable in sas based upon date - date

I want to create a categorical variable based upon date and input the following code.
data temppricedata;
set SASHELP.PRICEDATA;
date_group='';
IF (date>='MAR2002'd) THEN
date_group='new';
IF (date<'MAR2002'd) THEN
date_group='old';
run;
However I got error like
ERROR: Invalid date/time/datetime constant 'MAR2002'd.
ERROR 77-185: Invalid number conversion on 'MAR2002'd.
I am sure the format follows sas date format which is MONYY.
I do not know how to correct this.

As #Jeff mentioned, the correct way for specifying SAS date constants is DDMONYY or DDMONYYYY.
data temppricedata;
set SASHELP.PRICEDATA;
length date_group $3.;
IF date >= '01MAR2002'd THEN date_group='new';
ELSE IF date < '01MAR2002'd THEN date_group='old';
run;

Related

How can I create time in proper format to export to a netCDF file in MATLAB?

Data
I am trying to create a time dimension using this:
t1 = datetime(1901,1,1);
t2 = datetime(2016,12,31);
t = t1:t2;
And create a netCDF file using this
nccreate('prec.nc','Prec',...
'Dimensions',{'time' 42369 'lon' 135 'lat' 129},...
'Format', 'netcdf4');
What I have tried
ncwrite('prec.nc', 'time', t);
Error Message
Error using cast
Unsupported data type for conversion: 'datetime'.
Error in internal.matlab.imagesci.nc/write (line 778)
scale_factor = cast(1, class(varData));
Error in ncwrite (line 87)
ncObj.write(varName, varData, start, stride);
Question
How can I create a daily time dimension that I can write out to a netCDF file? What is the proper date type for this conversion?
NetCDF doesn't define a single native way of storing date/time values, but there are established conventions, as desribed here.
There are two strategies for storing a date/time into a netCDF variable. One is to encode it as a numeric value and a unit that includes the reference time, e.g. "seconds since 2001-1-1 0:0:0" or "days since 2001-1-1 0:0:0" . The other is to store it as a String using a standard encoding and Calendar. The former is more compact if you have more than one date, and makes it easier to compute intervals between two dates.
So you could:
a) Use datestr to convert it to a string value. The conventional date string format for data interchange is ISO 8601, which you can get in Matlab with datestr(myDateTime, 'yyyy-mm-ddTHH:MM:SS').
b) Convert it to a numeric value representing seconds or days since a reference "epoch" time. I'd suggest using the Unix epoch, since Matlab provides a convenient conversion function for this already: posixtime(myDateTime). Then specify your units for that variable in the NetCDF file as 'seconds since 1970-01-01 00:00:00'.
You probably want to make sure your datetimes are in UTC before encoding them in the NetCDF, so you don't have to worry about time zone issues.

Teradata Date calculation format issue

I have this date set uploaded from SAS to TD and the date format is like MEDI_START_DT format,'MM/DD/YYYY'
And now I want to make a calculation baesed on this column
MEDI_START_DT + PDAYSSUP -1 = MEDI_END_DT_PRE
but MEDI_END_DT_PRE's format is not a date and I tried cast(MEDI_END_DT_PRE's as date) and it says invalid date.
Do you guys have any idea on how to get a date format result after the calculation? Thanks!
enter image description here
If you loaded the data from SAS and it was formatted as a DATE in SAS then it should work. You should check if your PDAYSSUP variable got defined as FLOAT in Teradata. Try casting that variable:
select MEDI_START_DT + cast(PDAYSSUP as integer) -1 as MEDI_END_DT_PRE
from ...

how to manipulate date formats with phpexcel

I am using phpexcel to read specific set of data from a excel sheet into an array.
// example $cell_range = 'A2:AH35';
$cell_collection = $objPHPExcel->getActiveSheet()->rangeToArray($cell_range, NULL, True, TRUE);
In this sheet, two columns (L & N) contain dates formates as dd-mmm-yy (01-Jan-16). In order to upload this into database I need to convert this to format yyyy-mm-dd (2016-01-01).
I cannot simply change the last parameter of rangeToArray to FALSE (formatting retention), because that will just give me a set of x numbers instead of the date.
So how do if reformat the dates? Can be either on the level of phpexcel reading the data or by manupulating the resulting $cell_collection array.
You need to do it manually.... you know which columns contain dates, so return the "raw" MS Excel timestamp and use PHPExcel_Shared_Date::ExcelToPHP() or PHPExcel_Shared_Date::ExcelToPHPObject() to convert to a unix timestamp or DateTime object, and then you can use standard PHP date() function or the DateTime object's format() method to convert to a human-readable date string in whatever format you want.
Alternatively, reset to MS Excel numberformat mask to whatever date/time format you ant for those columns/cells before calling toArray().

Date display Appery.io

I use a date picker that saves the date in a yyyy-mm-dd format in a database. It then automatically adds time that always appears as 00:00:00. So for example it displays the date like this : 2014-12-14 00:00:00. I want it to display just the date in a mm-dd-yyyy format.
I use the code below but something seems to be wrong with it because it simply doesn't change the way it is displayed. I want to split up each of the values, then display only the date in a different format.
var parts = value.split("/");
return parts[1] + "-" +parts[2] + "-" +parts[0];
What can I do to make it work? Javascript please.
Thanks in advance.
It's a wide variety of solutions. Depends on you server-side settings too, but I will assume, that you use common for most websites MySQL DB and PHP.
You can change you column type to DATE. As said in docs:
The DATE type is used for values with a date part but no time part.
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The
supported range is '1000-01-01' to '9999-12-31'.
You can change your column type to VARCHAR and store a date in any format you like when INSERT it
You can convert it to proper format using MySQL built-in DATE_FORMAT()
SELECT DATE_FORMAT('2014-12-14 00:00:00', '%c-%e-%Y');
will give you 12-14-2014. Here is sqlfiddle,
just change first param to your column name in query
On server you can use PHP's date() function like this
$yourdate = '2014-12-14 00:00:00';
echo date("m-d-Y", strtotime($yourdate));
Closer to your question, how to do it in JS? It also has special object for this: Date(). Put your date in constructor, then use methods:
var yourdate = new Date('2014-12-14 00:00:00');
alert(yourdate.getMonth() + "-" + yourdate.getDate() + "-" + yourdate.getFullYear());
JSFiddle < - here
So, as far as almost every platform understands this datetime format - you can use any tool for converting it

Find smallest difference between dates SAS

I have this HW question that is asking me:
Write a SAS Program to creates a data set that contains test date closest to
delivery date. Your program must work for any test date and delivery date.
Here is what I have done so far. The data sources are in seprate sheets in excel which I p pulled in and merged and there is only 1 deliver date and 21 test dates. I figure the best way to find the closest day was the absolute value of the smallest difference, then use proc sort because that is the only proc command we are allowed to use other than proc import and export. Any ideas/help/whatever would be appreciated thanks.
proc import datafile = "C:\Users\file1.xls"
dbms=xls replace out=labs; sheet = "labs";;
run;
proc import datafile = "C:\Users\file1.xls"
dbms=xls replace out=delivery; sheet = "delivery";
run;
data dl;
merge delivery labs;
dd = delivery_date;
diff = dd - Test_date;
run;
Here is the data they are both in 1 column didn't know how to format that here.
Sheet 1:
delivery_date
11/16/2011
Sheet 2:
Test_date
13-Mar-11
10-Apr-11
20-May-11
9-Jun-11
31-Jul-11
17-Aug-11
12-Sep-11
10-Nov-11
11-Oct-11
12-Dec-11
29-Feb-12
13-Mar-13
10-Apr-10
20-May-10
9-Jun-10
21-Jul-11
15-Aug-11
15-Sep-11
19-Oct-11
21-Nov-11
22-Dec-11
It sounds like you are on the right track. Given that this is homework, I'm not going to give you a complete solution, but here are some components you may find helpful:
First, you should look at the SAS website for more information about the absolute function (since that is the route you want to take):
SAS/IML(R) 9.3 User's Guide: ABS Function
Next, you may want to review the documentation for PROC SORT. It will be useful for finding the smallest difference.
For getting only one record, you may find the OBS Data Set Option helpful.
(Hint: you may need to create a second dataset.)