How to change day/date/year in sas? - statistics

In my data lines, the data is 03/MAY/2012.
How can I make this data to 05/03/2012?
(Do not change data in data lines)
I don’t know how to do.

Use INFORMAT DATE. to specify how the data should be read in - so what format is the input in.
Use FORMAT MMDDYYS10. to specify how the data should be displayed
data have;
informat date date.;
format date mmddyys10.;
input date;
cards;
03/May/2012
;;;;
run;

Related

Converting timestamp to date in excel and SAS import

I am trying to extract the date from a date timestamp in excel. I currently have a data file with a mixture of date formats including date only and date timestamps. This is causing me problems as I am importing the data into SAS and it cannot read both the date only and date timestamps under the same column.
I have tried in Excel converting the timestamp to a date using the following formula:
=DATEVALUE(DAY(E32) & "/" & MONTH(E32) & "/" & YEAR(E32))
This works in excel and converts the date so that they are all formatted the same and therefore gets around the issue of the timestamp. However when I import the data into SAS, I get null values if the day is greater than 12, i.e. it is reading the date as mm/dd/yyyy. For example:
Excel Date SAS Import Date
09/12/2016 09/12/2016
15/12/2016 #VALUE!
I tried to reformat this in excel using the following to see if it would get around the issue:
=DATEVALUE(MONTH(E32) & "/" & DAY(E32) & "/" & YEAR(E32))
But I then get the same SAS error in excel.
Can anyone help suggest a formula to use in excel that will get around this issue or advice on importing the data into SAS?
It sounds like your Excel data is in DMY format, but SAS is using MDY. You can check SAS by running the following code :
proc options option=datestyle;
run;
If it is MDY, then change it (and if you're in the UK ask your SAS admin to change the default setting)
option datestyle='DMY';
You can also check the locale value, which in the UK will be EN_GB. This value determines the datestyle value used when working with dates.
proc options option=locale;
run;
If you asked SAS to import from an XLSX file then it should be able to tell that the column contains dates, independent of which display format you have attached to the cells. Make sure that all of the cells in a single column are the same type of data and use the same display format.
CSV files are not Excel files and so there is no place to put a formula or any metadata about what type of data is in each column. If you use PROC IMPORT to read the CSV file then SAS will have to guess at what type of data each column in the CSV contains. If you are saving an Excel files as a CSV file for later reading into SAS or other software then you should format your date columns using yyyy/mm/dd format in Excel to prevent confusion that can be caused by different defaults for month and day order. Nobody uses YDM order.
Since a CSV file is just a text file if you want complete control over how SAS reads the date strings then just write the data step to read it yourself. You could run PROC IMPORT and then recall the code that it generates and modify it to read your data. You could read the string into a character variable and then write your own statements to convert it using say the INPUT() function.
If the column has some date values and some date time values then you could try using the ANYDTDTE informat to pull just the date part. That informat should properly handle 15/10/2016 even if your LOCALE settings are for US or other locations where dates are normally represented in MDY order and not DMY order.
If your dates are consistently in the DMY order then use DDMMYY informat to prevent the LOCALE setting from causing PROC IMPORT or ANYDTDTE informat to convert 12/10/2016 to December 10th instead of October 12th. But if your text file actually has some rows with dates in month first order and others in day first order then you will really need some extra information to properly tell the difference between December 10th and October 12th.

How to force a column in excel with no values to import as a date column

I have an Excel file with a column that can be null or contain a date value, but when importing it into SAS I need the column to be in the format DATE9. As long as the column isn't completely null it works, but in some cases it will be and it imports into SAS as a $CHAR1.
The cell format is already date if that makes a difference.
Ok, I'm assuming you have SAS Access to PC File Formats licenced then. You should be able to use the data set option DBSASTYPE, which forces SAS to use the stated data type, irrespective of how the input data looks. In your case you want it set to numeric, then you can just use DATE9. to format it. The code should look something like below, I'm not in a position to test it right now.
DATA WORK.excel_file;
set excel.spreadsheet (dbsastype=(column_name=numeric));
format column_name date9.;
run;
I think you need to redefine the column after you have imported it.
Something like this:
data importedExcel(drop=suspiciousVariable2);
set importedExcel(rename=(suspiciousVariable=suspiciousVariable2));
attrib suspiciousVariable length=8 format=date9.;
suspiciousVariable=suspiciousVariable2 + 0;
run;
If you import with a data step instead of proc import, you can easily define the format of each variable. The following link gives a reasonably good example:http://www2.sas.com/proceedings/sugi30/038-30.pdf

How to convert timestampt into excel text

I have a timestamp (1394475248) when I translates using =(((Column/60)/60)/24)+DATE(1970,1,1) and the date format gives the timestamp (YYYY-MM-DD hh:mm:ss ) but I have to store it in a timestamp which looks like this "2014-03-10T23:11:09.000+0100" as all the previous data was in this format and changing would require a great deal of work/time.
Can someone tell what is the timestamp format and how do i convert the number to the exact format? many thanks
W
Try using TEXT function to convert to s strng in required format, e.g. with your original timestamp in A2 use this formula to convert and format
=TEXT(A2/86400+DATE(1970,1,1),"YYYY-MM-DDThh:mm:ss.000")&"+0100"

Date format YYYY-MM-DD+11:00 convert in to YYY-MM-DD HH:MM:SS

I Received excel data with following format
Date format YYYY-MM-DD+11:00 (Ex 2014-02-15+11:00 /2014-02-18+13:00)
Now I need to convert into this format
2014-02-18 HH:MM:SI
Please help me to do this
cheers
Just did a quick test. It's a little sloppy, but this formula will work. For the sake of argument, it assumes that the data you're trying to convert is in A1 of the current sheet, that it's all the same length, and that the format is "General":
=SUM(DATEVALUE(LEFT(A1,10)),TIMEVALUE(RIGHT(A1,5)))
Wherever you use the above formula, change the format to the Custom Format YYYY-MM-DD HH:MM:SI. I'm not sure what you're looking for with SI. I'm taking that literally. That custom format will display the seconds value and then the letter I. If you're looking for a different value, like milliseconds, then you can look that up. But if the data you're converting is in a "General" format, when you convert it, it won't have seconds or milliseconds, anyway. Those will all get converted to 00s.

Formatting Date(YY:MM:DD:Time) in Excel

I have an excel file, with a date column, but I want to convert the date column to
YY/MM/DD/Time
Ive been searching for 2 hours and no result yet.
This is my data:
Source Data: http://i.stack.imgur.com/75zbS.jpg
Expected Output: YY/MM/DD/Time
Can someone help me how I can do it? I want to insert it into postgresql and I want to change everything to compatible date format.
EDIT: I have tried Right Click -> Format cells -> date but it does not change anything!
Thanks
You could use this method and split the date and time into separate cells:
=DATE((LEFT(A1,4)),(MID(A1,5,2)),MID(A1,7,2))
=TIME(MID(A1,10,2),(MID(A1,12,2)),0)
Once your date value is in a format Excel can recognize, you can then change the formatting to whatever you'd like.
Or if you don't care to have the value in a recognizable date format, you can just get your desired formatting like this (will give you a string that looks like this: YY/MM/DD/Time):
=MID(A1,3,2)&"/"&MID(A1,5,2)&"/"&MID(A1,7,2)&"/"&MID(A1,10,4)
ISO 8601 format would be YYYY-MM-DD H24:MI:SS.
But you can set Postgres to accept various date styles by setting the datestyle setting. You can do that globally in postgresql.conf or temporarily for your session.
SET datestyle = SQL, DMY
For more exotic formats, you can create a temporary staging table, COPY to it and INSERT into your target table from there. Among others, you can use to_timestamp():
SELECT to_timestamp('13/10/14/17:33', 'YY/MM/DD/hh24:mi')
More info and example code in related answers like these:
Replacing whitespace with sed in a CSV (to use w/ postgres copy command)
How to bulk insert only new rows in PostreSQL
Your going to have to parse the date into four columns using fixed parsing.
Then reassemble the columns any way you want.
Just Google with excel parse columns fixed.

Resources