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;
@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.
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.
i need to convert it to year. format so i can use it to find an age in a later step.
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?
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
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.
I think @Cooksam13 wants to convert the variable to SAS-Date. Is there a year INFORMAT?
@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.
@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.
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.
@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.
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:
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.