BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TRESSQ
Fluorite | Level 6

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!Screenshot 2021-07-23 232852.png 

1 ACCEPTED SOLUTION

Accepted Solutions
jimbarbour
Meteorite | Level 14

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

 

View solution in original post

9 REPLIES 9
jimbarbour
Meteorite | Level 14

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:

jimbarbour_0-1627100728088.png

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

TRESSQ
Fluorite | Level 6

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;
jimbarbour
Meteorite | Level 14

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

TRESSQ
Fluorite | Level 6

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

jimbarbour
Meteorite | Level 14

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

 

TRESSQ
Fluorite | Level 6

Ok I will try it later tho. Thanks

TRESSQ
Fluorite | Level 6

Thank you so much. I will check it. Appreciate it.

Kurt_Bremser
Super User

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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 3945 views
  • 2 likes
  • 3 in conversation