DATA Step, Macro, Functions and more

create a yearmonth variable with YYMMn6. format

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

create a yearmonth variable with YYMMn6. format

I would like to create a new variable that take YYMMn6 format to display yearmonth, taking value from another numeric variable. 

For example, the original numeric variable has value 199312, the new variable would display the same value "199312".

So far I have tried:

format new_yearmonth = put(yearmonth, YYMMn6.);
* but this take yearmonth value as date value and convert this to a weird number 245603 or something;

I have successfully converted something similar to yearquarter format with the code:

format yearquarter YYQn6.;
yearquarter = YYQ(year,quarter);
*where year is a numeric variable having value 1993, quarter is also a numeric variable that has value 1, 2, 3, and 4;

So I guess I can also try this method but could not find a function similar to YYQ that takes value of 2 variable year and month. Is there such function?

thanks


Accepted Solutions
Solution
‎08-24-2017 12:02 AM
Trusted Advisor
Posts: 1,837

Re: create a yearmonth variable with YYMMn6. format

Posted in reply to ducman1611

You need first to convert the input numeric variable to a sas date variable.

Sas date contains: year, month and day. 

Having input like var = 201706 - the day is absent. You can choose any day to adjust to the input.

In such cases ususally we adjust day=01;

 

There are many ways to convert your input to a sas date.

In order to display the sas date as yyyymm assign to it the format  YYMMN6. 

Format new_date yymmn6.;

1) using MDY function - needs to split your input to year and month:

    

new_date = mdy(mod(var,100), 01, int(var/100) );
/* -------     month        day     year    -------*/

 

2) alternatively, convert the number given to a string and adjust the day:

   

new_date = input(put(var,6.),yymmdd8.);

 

3) The most simple way to convert the input to a sas date - which maybe you used is:

   

new_date = input(put(var,6.), yymmn6.);
format new_date yymmn6.;

View solution in original post


All Replies
Super User
Posts: 23,724

Re: create a yearmonth variable with YYMMn6. format

Posted in reply to ducman1611
This is really confusing. What do you have? What do you want?
Contributor
Posts: 61

Re: create a yearmonth variable with YYMMn6. format

I have a yearmonth variable whose format is numeric ( value could be 199311 or 201009)
I want a new variable whose format is YYMMn6 and displays values 199311 or 201009.
The purpose is that when I draw graph, there is no gap between 199312 and 199401
Solution
‎08-24-2017 12:02 AM
Trusted Advisor
Posts: 1,837

Re: create a yearmonth variable with YYMMn6. format

Posted in reply to ducman1611

You need first to convert the input numeric variable to a sas date variable.

Sas date contains: year, month and day. 

Having input like var = 201706 - the day is absent. You can choose any day to adjust to the input.

In such cases ususally we adjust day=01;

 

There are many ways to convert your input to a sas date.

In order to display the sas date as yyyymm assign to it the format  YYMMN6. 

Format new_date yymmn6.;

1) using MDY function - needs to split your input to year and month:

    

new_date = mdy(mod(var,100), 01, int(var/100) );
/* -------     month        day     year    -------*/

 

2) alternatively, convert the number given to a string and adjust the day:

   

new_date = input(put(var,6.),yymmdd8.);

 

3) The most simple way to convert the input to a sas date - which maybe you used is:

   

new_date = input(put(var,6.), yymmn6.);
format new_date yymmn6.;
Contributor
Posts: 61

Re: create a yearmonth variable with YYMMn6. format

Thank you so much. it works perfectly. 

Just a quick question. How do we compare the date? for example, if I want dates after 199702? Since it is not numeric, I cannot simply do >199702.

Thanks

Trusted Advisor
Posts: 1,837

Re: create a yearmonth variable with YYMMn6. format

Posted in reply to ducman1611

Sas date counts days since 01/01/1960 = 0;

If you convert the two input variables to date it is just simple numeric comparison.

 

Anyway, as your char-date is in yyyymm format, you can compare them as 

alphanumeric strings. If it was in mmyyyy format it will give false results.

Contributor
Posts: 61

Re: create a yearmonth variable with YYMMn6. format

Hi Shmuel,
While we are on this topic, I would like to create a variable that represent year and has format YEAR4. . This take values from a different variable that already shows years but in numeric format (best8.). How would I do this?
Thanks
Trusted Advisor
Posts: 1,837

Re: create a yearmonth variable with YYMMn6. format

Posted in reply to ducman1611

YEAR4. format is applicable with a sas date varibale. 

If your variable as a numeric year only (used with BEST4. format) then 

convert it with MDY function, adding any month or day and assign the YEAR4. format.

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 2601 views
  • 5 likes
  • 3 in conversation