BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
somebody
Lapis Lazuli | Level 10

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

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

7 REPLIES 7
Reeza
Super User
This is really confusing. What do you have? What do you want?
somebody
Lapis Lazuli | Level 10
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
Shmuel
Garnet | Level 18

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.;
somebody
Lapis Lazuli | Level 10

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

Shmuel
Garnet | Level 18

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.

somebody
Lapis Lazuli | Level 10
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
Shmuel
Garnet | Level 18

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 68188 views
  • 6 likes
  • 3 in conversation