Converting number into dates - sas

I have this number 90724 which is actually 24/07/2009, how can output the number to that date format. Another example 100821 should 21/08/2010
data want
set testData;
format date ddmmyyyy.;
run;
Cheers

You really need to learn about Informats. Another good introductory source is this UCLA site
What you really needed to specify is the format in which you have the dates - using the informat yymmdd6. It uses a YearCutOff option to determine which century a 2-digit year falls into see Adjusting Dates in a New Century & YEARCUTOFF= System Option
Note: The default is 1920 which spans the 100-year range between 1920 and 2019 - if your dates are outside this range then set the appropritate cutoff value using OPTIONS YEARCUTOFF=nnnn;
data test;
dateNumber=100821; ProperDate=input(put(dateNumber,6.), yymmdd6.); output;/*ProperDate= 21AUG2010*/
dateNumber=90724; ProperDate=input(put(dateNumber,6.) , yymmdd6.); output;/*ProperDate=24JUL2009*/
format ProperDate date9.;
run;

Related

Time Series Forecasting Technique, I have a Date Column which has data like this: 08JUL13:05:06:00

I want to forecast for next year, i have data for 2013. dates are in format:
08JUL13:05:06:00
for data preparation it takes intervals as minutes and whenever i change interval there is no output.
Also when Interval is in minutes my data has lot of missing values which i understand because this is billing data for entire year.
try datepart() function. it takes only date from date-time format
so datepart('08JUL13:05:06:00'dt) returns '08Jul13'd
data want;
data have
date_Column = Datepart(Date)
format date_Column date9.;
run;

how to read date and time from txt file using sas

I'm very new to SAS and I'm trying to read a txt file that contains date and time. The file is shown in the following figure
I believe I have tried all the possible options that I can think of to read the file but the output is still in numeric form. The following is the code I'm using
data wb_bg_1619;
infile "C:\Users\daizh\Desktop\Ren\SAS\wb_bg_0215.txt" firstobs=3 missover;
informat DATE DATE7. TIME TIME5. ;
input DATE TIME BG;
run;
proc print data=wb_bg_1619;
run;
The output looks like this
You've used an informat to automatically convert a date stored as text into a numeric SAS date format, which is the number of days since Jan 1 1960. To display that in a human readable format, you need to use a regular format. Add the following to the top or bottom of your code:
format date date9.
time time.
;
This changes how the data is displayed to you, but does not change how SAS works with it. As far as SAS is concerned, a date is only a number. You could run the rest of your program without ever using a format and get the right numbers and calculations with it if you wanted to, but that sure makes troubleshooting hard.
To remember the difference between a format and informat:
informats are for inputs
formats are for you

Passing a date parameter into a SAS code from table

I have to perform a number of processing for past 2 years (2017-2019) within SAS for every month.
I have a job that uses a YYMMDD parameter to indicate what data should be used from the data warehouse.
Lets say i have a table with JOB_NAME and JOB_DATE columns along with conditions (based on rc value the next one will start or not).
How can i tell SAS to take the date parameter from a certain column in a certain table?
You can just format after loading dataset...
data Want;
set have;
format JOB_DATE YYMMDD10. /*or use YYMMDD8.*/;
run;

How to categorize a date by decade in SAS

I have a dataset with many dates in them. I want to categorize these dates into a new column that organizes them by decade (1980s, 1990s, etc).
I have a good idea on how to use IF, AND, and ELSE statements to accomplish this, but I don't know how to have SAS extract the year and only the year from the date to apply it to the conditional logic.
You could always use multipliers and the intnx() function as well.
Using Allan's sample data...
data want;
set have;
decade=year(intnx('year10.',dateval,0,'beginning'));
run;
The intnx() part of the code returns the date corresponding to the start of the decade, then we just take the year portion from it.
The year10. parameter tells it we want to work with decades, the 0 parameter means shift the date supplied to the current decade, and the beginning parameter tells it to return the date corresponding to the beginning of the decade.
If you're not familiar with using intnx() to perform date calculations in SAS see here for a quick primer: https://stackoverflow.com/a/11211180/214994
No need for conditional logic - can use a combination of the year() and floor() functions with some simple arithmetic:
data have;
infile cards;
input dateval date9.;
cards;
01JAN2004
08FEB1996
07MAR1987
14SEP1982
;run;
data want;
set have;
decade=floor(year(dateval)/10)*10;
run;
Which gives:

convert character variable to time in SAS

I have a variable called post_time which is of character type
Type : Character
Length : 5
Foramt : $5.
Informat : $5.
eg: 00300 ,01250
How do I get time from this? can someone pls help me out?
need the time to look like 03:00AM 12:50PM
Need to display all the time in standard time zone
SAS will work directly with this via the HHMMSS informat.
data _null_;
x = input('02100',HHMMSS5.);
put x= timeampm9.;
run;
Any time zone concerns can be handled using either a time zone sensitive format, such as NLDATMTZ., and/or the TZONES2U or TZONEU2S functions, which work with DATETIME but may be able to work with your time values if you never go across a date boundary (though using timezones that's risky).
See the SAS Documentation page on Timezones for a more detailed explanation.
Assuming that the first three digits are hours and last two are minutes this may work:
data have;
input time $;
cards;
00300
00400
00234
;
run;
data want;
set have;
time_var=hms(
input(substr(time, 1,3), 3.),
input(substr(time, 4,2), 2.),
0);
format time_var timeampm8.;
run;