DATA Step, Macro, Functions and more

convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Accepted Solution Solved
Reply
Contributor
Posts: 29
Accepted Solution

convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Hi All, 

 

I've found a lot of posts almost aswering question but so far nothing addressing it directly: 

 

I have a character date variable called "mthdate" that is formatted like this YYYY-MM (eg. 2015-05)

 

Is there a straightforward way to transform this into a SAS date? Since I don't have the days in the dataset we can assume that 2015-05 is the first of the month (eg. 2015-05-01) 

 

any thoughts are always much appreciated. 

 

Thank you. 

 

Mike 

 

 


Accepted Solutions
Solution
a week ago
PROC Star
Posts: 1,558

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

[ Edited ]
Posted in reply to righcoastmike
data h;
input date $10.;
cards;
2015-05
;

data w;
set h;
new_date=input(date, anydtdte21.);
format new_date yymmdd10.;
run;

View solution in original post


All Replies
Solution
a week ago
PROC Star
Posts: 1,558

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

[ Edited ]
Posted in reply to righcoastmike
data h;
input date $10.;
cards;
2015-05
;

data w;
set h;
new_date=input(date, anydtdte21.);
format new_date yymmdd10.;
run;
Contributor
Posts: 29

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Posted in reply to novinosrin

That did the trick. Thank you. 

 

Is there a way to get it to display as just the SAS date #" 

 

eg. April 1 2011 = 18718

 

Thanks again. 

 

Mike 

 

 

PROC Star
Posts: 1,558

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Posted in reply to righcoastmike

do you mean without any format just as numbers or some other format?

Contributor
Posts: 29

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Posted in reply to novinosrin

Just as numbers I think. 

 

Here's the context:

 

Essentially I have panel data with 1 record per person in my study for each month of the study (study is 5 years so 60 records per individual).

 

The challenge that I am running into is that the "age" variable I was given is calculated as the persons age at the beginning of the study, and it is static (so if someone is 18 at the beginning of the study, the the value of "age" is 18 in all 60 records, even though they clearly aren't 18 after the first year.) 

 

I need to keep as many records as possible, so I'm hoping to generate a new variable that will update the given "age variable" every month.  Also, because I don't actually know the birthdays of my participants, for the purposes of creating this variable, everyone's birthday is July 2nd. So on July 2nd everyone gets a year older. 

 

It's a bigger question than I originally asked, but I thought the broader context might be helpful.  If you care to offer any potential solutions I'd be eternally grateful. 

 

I'm not sure if that's enough info, happy to give you anything else you need.

 

Mike

 

 

PROC Star
Posts: 1,558

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Posted in reply to righcoastmike

Basically, a sas date is a numeric date and a format merely is a display of the numeric date. So there is no change in the date value(number) regardless of what format you apply,

If you just want to display as numbers, simply do not apply a format, nonetheless this does;t change anything. 

 

remove this 

format new_date yymmdd10.;

from the program. 

 

Anyways, for your bigger challenge, I would request you to please open up a new thread with a comprehensive input sample data with an expected output sample data and of course with a words explaining the business logic. This will help res-ponders to take a stab at it.

 

Feel free to post any number of questions no matter how complex you deem it to be. That;s what the forum is for. All fun.  

Contributor
Posts: 29

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Posted in reply to novinosrin

Thanks so much Novino, both for your help on this one and the suggestion to start a new post for the larger problem. 

 

I've started a new thread over here: 

 

https://communities.sas.com/t5/Base-SAS-Programming/Trying-to-create-a-dynamic-age-variable/m-p/4693...

 

Thanks again. 

 

Mike

PROC Star
Posts: 1,558

Re: convert yyyy-mm to date, assuming yyyy-mm date is the 1st of the month

Posted in reply to righcoastmike

Yes sir, saw that will be on it in a bit. I am sure that helps other SAS champs too. Just an idea to make it convenient. Thanks so much!

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 106 views
  • 0 likes
  • 2 in conversation