I read in a sas7bdat file using the haven package.
One column has dates in the YYQ6. format.
I R this is converted to numbers like -5844, 0, 7121, ..
How can I convert this to a year format? I have no access to SAS but these values should be birth dates.
Bit of Research first. SAS uses as Zero 1st of January 1960 (see http://support.sas.com/publishing/pubcat/chaps/59411.pdf) so if you want the year of the data (represented by your number) it should be
format(as.Date(-5844, origin="1960-01-01"),"%Y")
and you get in this case
1944
is that correct? is what you are expecting?
To learn more on the data type YYQ6. check this Support article from SAS
http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#n02xxe6d9bgflsn18ee7qaachzaw.htm
Let me know if is working.
Umberto
I think in SAS dates are days from 1/1/1960, so you can do
sasq <- c(-5844, 0, 7121)
as.Date(sasq, '1960-01-01')
[1] "1944-01-01" "1960-01-01" "1979-07-01"
Related
I am starting with a date_of_survey variable that is a string formatted as YYYY-MM-DD. I then run the following commands to convert it to a date variable, and display that variable in a useful format:
gen date = date(date_of_survey, "YMD")
gen date_clean = date
format date_clean %dM_d,_CY
drop date_of_survey
That leaves me with a "date_clean" variable displayed as "September 3, 2020" and a corresponding "date" variable displayed as "22161" (equal to days since January 1, 1960).
I now need to create a variable that indicates the year and quarter of each observation, preferably in YYYY-QQ format. I assumed this shouldn't be difficult, but no matter how I have coded it, I wind up with years in the 7000s and inaccurate quarters. I must be misunderstanding how the dates are stored. My first instinct was to try a simple format date %tq command, but I'm still not getting the output I need. Any help is much appreciated. I read over the help files, and can't find the discrepancy that's causing this little problem.
ANSWER: I needed to put the date variable into quarters since January 1, 2021. a qofd() function call before the format %tq did the trick!
still new to R. I wanted to create a simple (bar) chart of the fluctuations/occurrences of burglaries per month in my city. I found that the column, 'Occurence_Date' is a character, I wanted it to be "time", or something simpler, to create a visualization. I wanted the "x-axis" to be the months of January to June 2019, with the "y-axis" to be the amount of burglaries per month. Can anyone help me get started on this please? Thanks!
This is my data frame
The lubridate package is very helpful for working with dates and times in R.
# load.packages("lubridate") ## only run once
library(lubridate)
df$Occurence_Date <- ymd(df$Occurence_Date) # converts text in year month day format, igrores time
Generally it's better to put example data in your question so people can work with it and show an example.
How can set R to count months instead of dates when converting integers to dates?
After reading several threads on how to convert dates in R, it seems like nobody has asked how it is possible to convert numeric dates if the numerics is given in monthly timeseries. E.g. 552 represents January 2006.
I have tried several things, such as using as.Date(dates,origin="1899-12-01"), but I reckognize that R counts days instead of months. Thus, the code on year-month number 552 above yields "1901-06-06" instead of the correct 2006-01-01.
Sidenote: I also want the format to be YEARmonth, but does R allow displaying dates without days?
I think your starting date should be '1960-01-01'.
anyway you can solve this problem using the package lubridate.
in this case you can start from a date and add months.
library(lubridate)
as.Date('1960-01-01') %m+% months(552)
it gives you
[1] "2006-01-01"
you can display only the year and month of a date, but in that case R coerces the date into a character.
format(as.Date('2006-01-01'), "%Y-%m")
I have an Excel-file with a lot of dates in the format "ww-yyyy". When it is loaded into R it is seen as "42370" (for instance).
I need to extract the week and year for each data point x.
I have tried as.POSIXct,as.POSIXlt, as.Date(x, format="%w-%y,origin = 1899-12-30).
as.Date(42370,format =%w%y) gives the output "2023-11-14" which should've been week 53 in 2015
The question is quite simple: I have a txt data imported in R. However, I forgot to change the date format to dd/mm/yyyy. For example: instead of having 30/09/2015 I have 42277.
Of course I could go back to my excel and change the column format from number to date and get the dd/mm/yyyy format easily. But I was thinking if there is a way of doing that inside R. I have several packages here, such as XLConnect but there is nothing there.
Here's how to convert Excel-style dates:
as.Date(42277, origin="1899-12-30")
The help file for as.Date discusses the vagaries of conversion from other time systems and includes a discussion and example for Excel.
## Excel is said to use 1900-01-01 as day 1 (Windows default) or
## 1904-01-01 as day 0 (Mac default), but this is complicated by Excel
## incorrectly treating 1900 as a leap year.
## So for dates (post-1901) from Windows Excel
as.Date(35981, origin = "1899-12-30") # 1998-07-05
## and Mac Excel
as.Date(34519, origin = "1904-01-01") # 1998-07-05
## (these values come from http://support.microsoft.com/kb/214330)