DATA Step, Macro, Functions and more

repace day 01 with day 15 in date variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 318
Accepted Solution

repace day 01 with day 15 in date variable

Hello, everyone

 

My variable birth_info has yyyy-mm format, and when I imported it into SAS, it was automatically converted to 01MMYY 00:00:00 format.

 

Is there a way to control the import format?

 

I actually want to use 15th of the month instead of the 1st day of the month as the day to calculate the ages. What is the easy way to replace all "01"s in the 01MMYY new date to "15"?

 

Thank you very much


Accepted Solutions
Solution
‎12-21-2015 09:26 PM
Super User
Posts: 19,772

Re: repace day 01 with day 15 in date variable

Posted in reply to fengyuwuzu
intnx the variable.

new_var=intnx('day', variable, 15);

View solution in original post


All Replies
Solution
‎12-21-2015 09:26 PM
Super User
Posts: 19,772

Re: repace day 01 with day 15 in date variable

Posted in reply to fengyuwuzu
intnx the variable.

new_var=intnx('day', variable, 15);
Super Contributor
Posts: 318

Re: repace day 01 with day 15 in date variable

Thank you for the quick reply.
It works and I got the age correctly.
Super User
Posts: 5,499

Re: repace day 01 with day 15 in date variable

Posted in reply to fengyuwuzu

It's important to understand what the variable actually contains.  From  your description, it looks like it is a numeric date-time variable.  If so, this will convert it to the proper date, while moving from the 1st to the 15th:

 

birth_info = datepart(birth_info) + 14;

format birth_info yymmdd10.;

 

After this, you will have a date variable (no longer date-time).

 

Try printing a few records after making the change, and see if the results look right.  If they're not right, we'll have to get a better handle on what the variable actually contains (results from PROC CONTENTS, results from PROC PRINT with all formats removed).

Super Contributor
Posts: 318

Re: repace day 01 with day 15 in date variable

Posted in reply to Astounding
Yes, 14 is the number to use if I want to replace with 15. Below are my codes:
new_birth=intnx('day', datepart(Birth_info), 15);
age = round(yrdif(new_birth, datepart(Registration_Time),'AGE'),0.1);
format new_birth mmddyy10.;
Super User
Posts: 5,499

Re: repace day 01 with day 15 in date variable

Posted in reply to fengyuwuzu

You'll have to see which code gives you the right result.  I don't think that my answer and Reeza's answer are compatible (although I could be proven wrong).  I assumed you had a date-time, and I think Reeza assumed you had a date.  So check the results and see which one is working properly.

Super Contributor
Posts: 318

Re: repace day 01 with day 15 in date variable

Posted in reply to Astounding
In my .csv file I have yyyy-mm format date variable, and when I imported into SAS, it became 01MMYY 00:00:00 format.

I got the ideas from both of you so I think I am okay. Thank you again!
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 301 views
  • 1 like
  • 3 in conversation