I am new to SAS and I am struggling struggling with my code. I would love some help. Am I thinking about this the right way? I have a huge table and I want to extract that data from certain dates. My two dates: 1969-12-01 and 1948-01-01 my sample code:
data null;
call symput ('timenow',put (time(),time.));
call symput ('datenow',put (date(),date9.));
run;
title "The current time is timenow and the date is datenow";
proc print data=sashelp.buy;
run;
So first learn about your dataset. So for example run PROC CONTENTS.
proc contents data=sashelp.buy; run;
Which will show you that there is variable named DATE that has date values (number of days since 1960).
So to reference a specific date use a date literal. That is a quoted string in the style that the DATE informat can read followed by the letter D. You can then use a WHERE statement to filter the data.
data want;
set sashelp.buy;
where date = '31dec1969'd ;
run;
Which will not find any observations since that date does not appear in that dataset.
If you want to select for multiple dates you could either add more conditions using OR.
where (date = '31dec1969'd) or (date = '01jan1948'd);
You can also use the IN operator:
where date in ('31dec1969'd '01jan1948'd);
Note that if your variable contains datetime values (number of seconds) then to pick a specific date you would either need to use a range of datetime literals:
where datetime between '31dec1969:00:00'dt and '31dec1969:11:59:59'dt);
Or convert the number of seconds into number of days and compare to the date literal.
where datepart(datetime) = '31dec1969'd ;
Welcome to StackOverflow Sportsguy3090.
Here I make a dataset called sample with some sample dates. That dataset has a variable called name and another variable called date. Internally, SAS stores dates as the number of days until or after January 1st 1970. That is rough to look at. So I use the format statement to have the dates appear as a 10 character string with month/day/year.
data sample;
name = "Abe "; date = "01Dec1969"d; output;
name = "Betty"; date = "01Jan1948"d; output;
name = "Carl"; date = "06Jun1960"d; output;
name = "Doug"; date = "06Dec1969"d; output;
name = "Ed"; date = "01Jan1947"d; output;
format date mmddyy10.;
run;
The code below subsets the data and puts the good records into a new dataset called keepers. It only keeps the records that are in the date range (including the limit dates).
data keepers;
set sample;
where date between "01jan1948"d and "01Dec1969"d;
run;
I hope that helps.... if not send up another flare.
I have a data in format 01 Jan 19.00.00 (datetime), and I want to extract the month name only from it.
Tried the below code but getting output in numbers i.e. 1 2 3 and so on. I want the output either in Jan Feb mar format or January February march format.
data want;
set detail;
month = month(datepart(BEGIN_DATE_TIME));
run;
You can use the MONNAME format.
data test;
dt = datetime();
monname = put(datepart(dt),MONNAME.);
put monname=;
run;
If you want "OCT" not "OCTOBER" you can add a 3 to the format (MONNAME3.).
If you are using the value in a report the better approach might be to use a date value formatted with MONNAME.
The values of a date formatted variable will be ordered properly when the variable is used in a CLASS or BY statement. If you had instead computed a new variable as the month name, the default ordering of values would be alphabetical.
data want;
set have;
begin_date = datepart(BEGIN_DATE_TIME);
format begin_date MONNAME3.;
run;
Original Value is in DATETIME25.6
I need to change it to YYYYMMDD
I am using the below in a datastep;
'Date of Birth'n = put(borrower_dob,yymmddn8.);
However it returns ******** as the value. Help!
The date part of a SAS datetime value can be output with the format B8601DN8.
From docs
B8601DNw.Format
Writes dates from datetime values by using the ISO8601 basic notation yyyymmdd.
data want;
dtnow = datetime();
format dtnow datetime20.;
put dtnow= B8601dn8. 'is the yyyymmdd rendering of ' dtnow=;
yyyymmddstring = put (dtnow, B8601DN8.);
run;
----- LOG -----
dtnow=20191003 is the yyyymmdd rendering of dtnow=03OCT2019:10:02:14
You need just the date part(days) of the date time(seconds)variable. Check the documentation for explanation of SAS date and date-time variables.
'Date of Birth'n = put(DATEPART(borrower_dob),yymmddn8.);
I have a question. I have a field in my SAS dataset that has a mixture of datetime and date variables.
The field is PST_DT and is Type: Numeric. Group: Date. Format: Date9. Length: 8.
Some values look like this:
PST_DT
8/22/2018 11:59:59 PM
8/22/2018
How can I turn just the datetime values in date format? I want all the values to be in date format.
Thanks.
The question does not make much sense as a date variable (number of days since 1960) cannot have a time component at all, much less have it selectively.
If you have a DATETIME value (number of seconds since 1960) and want to convert it to a date value use the datepart() function. And attach a date format so that the value displays in a human friendly way.
pst_date = datepart(pst_dt);
format pst_date yymmdd10. ;
If you have instead a character string then use the ANYDTDTE. informat to convert it to a date value.
pst_date = input(pst_dt,anydtdte40.);
format pst_date yymmdd10. ;
I have a datetime value in GMT timezone. How can I convert it to my local timezone?
I would expect there to be a function for this. Please note that I can not just add or subtract the difference, because of the summertime.
For example the function could work like this:
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
localdatetime=tz2local(gmtdatetime,"GMT");
run;
I tried some combinations of formats and informats without luck:
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
a=put(gmtdatetime,E8601DZ20.0);*Converts the value to "2014-09-17T09:42:10Z" to indicate that it is GMT;
localdatetime=input(a,B8601DT.);*Reads the GMT value;
put localdatetime datetime.;*This still prints the value as the original GMT value...;
run;
Thanks,
Stig
I made a function that would return the GMT offset, but when I compiled it, I got this error:
ERROR: Built-in SAS FUNCTION or SUBROUTINE already exists with name 'GMToff'.
It turns out, there is an undocumented function in SAS that returns the GMT offset and by luck I chose the same name! Here are some examples of usage.
Anyways, it will not return the offset on a specific time, as I wanted, only for the current time.
Here is a function that will convert the datetime to "local" time, given a timezone (only supports GMT, but adding additional timezones as needed should be trivial):
proc fcmp outlib = Apfmtlib.funksjoner.localtime;
function localtime(datetime,tz$);
if upcase(tz)="GMT" then do;
offset_normal=3600;
offset_summer=7200;
end;
localtime=datetime+offset_normal;
/*If datetime is between 1 AM the last Sunday of March and 1 AM the last Sunday of October it is "summertime" in central Europe:*/
if intnx('week',mdy(3,31,year(datepart(datetime))),0)*86400 + 3600 le datetime le intnx('week',mdy(10,31,year(datepart(datetime))),0)*86400 + 3600 then localtime=datetime+offset_summer;;
return(localtime);
endsub;
quit;
options cmplib = Apfmtlib.funksjoner;
/*Usage examples:*/
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
localdatetime=localtime(gmtdatetime,"GMT");
put localdatetime datetime.;
gmtdatetime="17DEC14:09:42:10"dt;
localdatetime=localtime(gmtdatetime,"GMT");
put localdatetime datetime.;
run;
I have a datetime value in GMT timezone. How can I convert it to my
local timezone? I would expect there to be a function for this. Please
note that I can not just add or subtract the difference, because of
the summertime.
There is a function for that:
TZONEU2S Function
Converts a UTC date time value to a SAS date time value.
http://documentation.sas.com/?docsetId=nlsref&docsetTarget=n1ien0skr1u9swn1f00w7hizdg9c.htm&docsetVersion=9.4&locale=en
When you specify a zone ID, the time zone that SAS uses is determined by the time zone name and daylight savings time rules.
Example:
data;
do d = 20 to 30;
date = '28FEB2018'd + d;
dt = dhms(date, 12, 0, 0);
dt2 = tzoneu2s(dt,'EUROPE/LONDON');
output;
end;
format date E8601DA. dt dt2 E8601DT. ;
run;
proc print;
run;
In SAS 9.4 the function tzonesoff is available that I think may be the answer to your question. The function returns the difference between your timezone and GMT.
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
tzoffset = tzoneoff('Europe/Copenhagen');
localdatetime=gmtdatetime+tzoffset;
put localdatetime= datetime.;
run;
You can see a list of timezones here.
If your timezone observes daylight savings time then it is very important that you choose the 'Time Zone Information' column to specify your timezone. Don't choose the abbreviation else DST will be ignored. For example, if you live in Los Angeles, choose 'America/Los_Angeles' not 'PST' or 'PDT' which would hardcode the conversion to either -7 or -8 hours different.
You can see the difference between them here:
data utc_dst_test;
format midnight utc datetime22.;
do date = '01jan2019'd to '31dec2019'd;
midnight = dhms(date,0,0,0);
utc = tzones2u(midnight,'America/Los_Angeles'); * ADJUSTS BASED ON DAYLIGHT SAVINGS TIME;
utc = tzones2u(midnight,'PST'); * ALWAYS 8 HOURS DIFF;
output;
end;
run;
If you are working with historical time values, use the tzones2u() function as shown above. If you just apply an offset to everything it won't be calculating the DST appropriately. Also be aware that tzones2u() is a very slow function to call... if you start to notice your programs running slow check to see how often this is being called.
I made a format that will give me the offset from GMT to my timezone (in seconds). In central Europe Daylight Saving starts the last Sunday in March at 1 am GMT and ends the last Sunday in October at 1 am GMT.
data fmt(drop=year);
attrib hlo length=$1
start end format=datetime.;;
fmtname="gmtoff";
type="N";
do year=1980 to 2080;
start=intnx('week',mdy(3,31,year),0)*86400 + 3600;*Last Sunday in March 1 AM;
end=intnx('week',mdy(10,31,year),0)*86400 + 3600;*Last Sunday in October 1 AM;
label=7200;*Two hours offset in summertime;
output;
end;
start=.;end=.;
hlo="O";
label=3600;*When it is not summertime, it is one hour offset;
output;
run;
proc format cntlin=fmt;
run;
/*Example of usage:*/
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
localdatetime=gmtdatetime + put(gmtdatetime,gmtoff.);
put localdatetime datetime.;
gmtdatetime="17DEC14:09:42:10"dt;
localdatetime=gmtdatetime + put(gmtdatetime,gmtoff.);
put localdatetime datetime.;
run;