SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

SAS Date MONYY problems

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

SAS Date MONYY problems

[ Edited ]

Hi, I have a problem using a  date with monyy5 format.

I created the from a ddmmyy10. using this code:

 

FECHA2=put(fecha,monyy7.); /*FECHA2 is the original date variable with ddmmyy10. format*/
MES=input(fecha2,monyy7.); /*MES is my variable for an only month year date*/

format MES monyy5.; /*Format for my new date variable*/

 

After that, I have a variable that only has month and year, but my problem is when I want to do a select distinct. For example, for the original date variable has two dates in april: 10/04/2017 and 18/04/2017. The new variable shows apr17 for both obs. When I run a select distinct query, it should appear only 1 obs showing apr17 (the rest of the variables are the same), but this not happens... I think internally SAS keeps the day of the date? 

 

How could I created a date variable that only has month and year? Thanks in advance!

 


Accepted Solutions
Solution
3 weeks ago
Super User
Super User
Posts: 8,190

Re: SAS Date MONYY problems

Yes, so dates are always held in SAS as days since a certain timepoint, regardless of format you apply to the number, you will always have the days since a timepoint number.  So you code is effectively:

format fecha monyy5.; 

You can put it to character and read it back in as much as you like, its not going to change the underlying number.  You cannot have a month/year variable, as dates always need to be full - see above as day is needed.  If you need to use only month and year, have two separate variables;

yr=year(fecha);
mnth=month(fecha);

And use those, or just convert to text and group on that:

fecha2=put(fecha,monyy7.);

View solution in original post


All Replies
Solution
3 weeks ago
Super User
Super User
Posts: 8,190

Re: SAS Date MONYY problems

Yes, so dates are always held in SAS as days since a certain timepoint, regardless of format you apply to the number, you will always have the days since a timepoint number.  So you code is effectively:

format fecha monyy5.; 

You can put it to character and read it back in as much as you like, its not going to change the underlying number.  You cannot have a month/year variable, as dates always need to be full - see above as day is needed.  If you need to use only month and year, have two separate variables;

yr=year(fecha);
mnth=month(fecha);

And use those, or just convert to text and group on that:

fecha2=put(fecha,monyy7.);
Contributor
Posts: 60

Re: SAS Date MONYY problems

Thank you! I will have to change my code.
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 125 views
  • 0 likes
  • 2 in conversation