Hi, Everyone. I want to know how to convert birthday age in the birth_days column. I have no idea how to type the code for that. Thank you!
Hmmm. I'm not sure that will give you what you need. Try this:
today = DATE();
days = today - birth_days;
age = floor(days / 365);
Jim
Well, you really don't to convert. The best way to store dates is as a number, just as you already have. Then, if you need to have a character date in human readable form, you just apply a Format.
Here's an example:
DATA Have;
FORMAT Birth_Days YYMMDDD10.;
INFILE DATALINES MISSOVER;
INPUT Birth_Days;
DATALINES;
-12005
-21474
-19110
;
RUN;
Here's what it looks like in my Output Data window:
I actually haven't changed the values. They're still a number. What I've done is apply a Format so I can read them.
If I truly need the dates in a character variable (maybe to show on a report), I can, again, apply a format, something like this:
Report_Date = PUT(birth_days, YYMMDDD10.);
Report_Date will be a character date, for example 2021-07-23.
Jim
Thank you so much! Jim. I was wondering if I want to show all the character date in human-readable form, should I copy all the data from birth_days colunm in the code and run it?
DATA Have;
FORMAT Birth_Days YYMMDDD10.;
INFILE DATALINES MISSOVER;
INPUT Birth_Days;
DATALINES;
-12005
-21474
-19110
-15519
-22464
(and so on, copy all the data inside here)
;
RUN;
Well, if you already have a SAS dataset, you could just apply a Format, and in the Data window, you'd have everything in machine readable format. No need to type in all the dates if they're already in a SAS dataset.
Example:
PROC DATASETS LIBRARY=WORK NOLIST;
MODIFY HAVE;
FORMAT Birth_Days DATE9.;
QUIT;
You'd have to change the Library from WORK to whatever Library it is that you're using, and you'd have to change the dataset name I'm using, Have, to whatever the name of the dataset that your using is.
Jim
Alright. One more question, I was thinking if I want to divide 365 by each data(ex: -12005/365 days), what kind of code should I search in Google?I thought that it was a good way to know their age. Appericate it! Jim
Hmmm. I'm not sure that will give you what you need. Try this:
today = DATE();
days = today - birth_days;
age = floor(days / 365);
Jim
Ok I will try it later tho. Thanks
Thank you so much. I will check it. Appreciate it.
DATALINES allows placing of input data inline in the code, it is mostly used to create small datasets for demonstration purposes or, say, CNTLIN datasets for PROC FORMAT.
When posting questions, always use the datalines method, as it enables us to quickly replicate your dataset with just copy/paste and submit. Posting a picture is the worst way of presenting data, as it does not tell us anything about variable attributes, and forces us to tediously type stuff off the screen, with ample opportunity for mistakes.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.