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

Hiyya, 

 

I cannot for the life of me figure out how to convert this data to year format

Dt.Birth is in format Best12, and I want to change it to year format

 

i have tried several different ways but it isn't working,

this is what I have now and then all the data gets set to 1965 which is not correct. 

data work.NCdate;
set new.step2;
Dt_b=input(input(Dt_birth, best.), year.);
format Dt_b year.;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@Sajid01 wrote:

Hello @Cooksam13 
I agree with what @PaigeMiller has said. You should be able to use the year value as it is.
Needless to say that SAS stores dates as numbers (number of days since 01Jan1960). The formats are just visual representations. Internally the date values continue to be in numbers regardless of the format you are using.


Yes, that last sentence is a great point. It doesn't matter* what format you wind up using (or even if you use no format), any future arithmetic or logical operations will be done on the un-formatted value. Changing the value of the variable (so that it is now a true SAS date value, as suggested above) is not a FORMAT issue.

 

* — okay the format matters if you are going to put the value in titles, labels or file names, or have to put it into a database that requires a specific format.

--
Paige Miller

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

Dt_birth is best12. — okay. What does it look like when you see it? Show us a typical example(s) of this variable's value.

--
Paige Miller
Cooksam13
Fluorite | Level 6

Cooksam13_0-1650315270171.png i need to convert it to year. format so i can use it to find an age in a later step.

 

 

PaigeMiller
Diamond | Level 26

Now I'm lost.

 

You want to convert DT_BIRTH which has a value of 1994 to a year? It is a year, isn't it?

--
Paige Miller
Cooksam13
Fluorite | Level 6

the format for Dt_birth is in best12 and I need to convert it to year. format to use it to find age in a later step

PaigeMiller
Diamond | Level 26

You want to format 1994 to be the year 1994 for later use? As far as I can see, you can use it as is, without any additional formatting or conversion.

 

What later use? Please be specific and detailed.

--
Paige Miller
data_null__
Jade | Level 19

I think @Cooksam13 wants to convert the variable to SAS-Date.  Is there a year INFORMAT?

PaigeMiller
Diamond | Level 26

@data_null__ wrote:

I think @Cooksam13 wants to convert the variable to SAS-Date.  Is there a year INFORMAT?


I think the MDY function would work. But @Cooksam13 didn't say that's what he wanted, maybe its what he wants. But he could simply use the variable as is too.

--
Paige Miller
ballardw
Super User

@Cooksam13 wrote:

the format for Dt_birth is in best12 and I need to convert it to year. format to use it to find age in a later step


To calculate age based on date of birth and a calendar date, assuming that is your goal, you need a day of month, month of year and a year value to create a date value if you want to use any of the date formats or functions.

For instance to make a value of 15 Jun 2020, the 15th day of the 6th month in 2020:

data example;
   date = mdy(6,15,2020);
   format date date9.;
run;

If you want to use just a year, what day and month do you want to use? Or show a complete working example.

Sajid01
Meteorite | Level 14

Hello @Cooksam13 
I agree with what @PaigeMiller has said. You should be able to use the year value as it is.
Needless to say that SAS stores dates as numbers (number of days since 01Jan1960). The formats are just visual representations. Internally the date values continue to be in numbers regardless of the format you are using.

 

 

PaigeMiller
Diamond | Level 26

@Sajid01 wrote:

Hello @Cooksam13 
I agree with what @PaigeMiller has said. You should be able to use the year value as it is.
Needless to say that SAS stores dates as numbers (number of days since 01Jan1960). The formats are just visual representations. Internally the date values continue to be in numbers regardless of the format you are using.


Yes, that last sentence is a great point. It doesn't matter* what format you wind up using (or even if you use no format), any future arithmetic or logical operations will be done on the un-formatted value. Changing the value of the variable (so that it is now a true SAS date value, as suggested above) is not a FORMAT issue.

 

* — okay the format matters if you are going to put the value in titles, labels or file names, or have to put it into a database that requires a specific format.

--
Paige Miller
mkeintz
PROC Star

First the YEAR. format can not be used as an INFORMAT, which is what you are apparently trying to do.

 

The YEAR. format will take a sas date-value (i.e. the actual number of days after 01jan1960 - or number of days before when the value is negative).   But thhe number 1994, when used as a sas date value would be interpreted as 17jun1965.  And if you applied the format (not informat) YEAR., it would be displayed as 1965.

 

So, if you have the value 1994, and for some reason you want to convert it to a sas date value where 1994 is the year in question, you have to choose among 365 possible date values, namely 01jan1994 through 31dec1994.  To do that you might use a statement like

Dt_b=mdy(1,1,dt_birth);

which takes the value 1994 and assigns it the numerical value representing 01jan1994.  Only then can you appropriately apply the YEAR. format to the variable dt_b, which will display the value 1994, even though the underlying number is 12419  (01jan1994 is 12,419 days after 01jan1960).

 

In summary two errors:

  1. YEAR. is a format (useful in a PUT statement of PUT function) not an informat (used in INPUT function or statement).
  2. To convert it to a sas date value (which is required if you intend to use a date-related format), you have to supply a month and day-of-month in addition to the year value that you already have in hand.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 11 replies
  • 966 views
  • 3 likes
  • 6 in conversation