BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Cruise
Ammonite | Level 13

I have a SAS date in format of YYYYMM as shown in my viewtable below. This YYYYMM "dob" variable is the truncation of original 8 digit SAS date for confidentiality purpose. So, I don't know how it originally looked like.

 

my data view

 

The final result that I would like to have is:

 

Aiming to come up:

 

I tried several different ways and some of my unsuccessful attempts are here: 

 

dob1 = put(dob, YYMMN6.); 
dob1 = input(dob, yymmn6.); dy = substr(dob,5,4); dm = substr(dob,7,2);

Jagadishkatam's suggested code below was resulted as shown:

dob1=put(dob,yymmn6.);
dy = year(dob1);
dm = month(dob1);

Result of above code:

results2.png

 

I haven't tried substrn yet. I will soon.

 

This problem was previously discussed here. But it didn't work with my SAS9.4.

 

Any hints are appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

What does 189005 mean in terms of a date? 

What part is the year, day and month? 

 

Try SUBSTRN

 

year = substrn(old_var, 1, 4);
month = substrn(old_var, 5, 2);

Or

 

Date_type =input( put(old_var, 6.), yymmn6.);
Year=year(date_type);
Month = month(date_type);

 

If this doesn't work post the exact code and log. 

View solution in original post

15 REPLIES 15
Jagadishkatam
Amethyst | Level 16

We could use the year() and month() functions as below

dy=year(dob);
dm=month(dob);

 

Note: At the moment the date seem not right like the DOB of 189005 when converted to date9 format it is 23Jun2477. so it is a strange date. but if it is only for example then it is fine.

Thanks,
Jag
Cruise
Ammonite | Level 13

Hi Jagadishkatam,

The solution you suggested was resulted as shown below. Your observation using data9. is very interesting. it's not just for display. I really have to figure out this yyyymm date variable. Current yyyymm date I have is the result of a truncation of 8 digit SAS date, I think.

output

Jagadishkatam
Amethyst | Level 16
you could try the

using the put(dob,yymmn6.) it does retrieve the year and month. I am not sure what is the exact issue you have.
Thanks,
Jag
Reeza
Super User

What does 189005 mean in terms of a date? 

What part is the year, day and month? 

 

Try SUBSTRN

 

year = substrn(old_var, 1, 4);
month = substrn(old_var, 5, 2);

Or

 

Date_type =input( put(old_var, 6.), yymmn6.);
Year=year(date_type);
Month = month(date_type);

 

If this doesn't work post the exact code and log. 

Cruise
Ammonite | Level 13
Your code did the trick to a month. But years are still in 1809 something range. I will post exact details when i get back to office. I have a feeling that yours is getting close.
Cruise
Ammonite | Level 13

Hi Reeza,

I tried your first code with and without left.

/*Reeza1*/
data x1; set x;
year = substrn(left(dob), 1, 4);
month = substrn(left(dob), 5, 2);
run; 
/*Reeza2*/
data feb.gro_id1; set feb.gro_id;
Date_type =input( put(dob, 6.), yymmn6.);
Year=year(date_type);
Month = month(date_type);
run; 

The corresponding results in viewtable are:

Reeza1:

Reeza1

Reeza2:

reeza2

Astounding
PROC Star

It would be helpful to have an example of the math.  For example, show what YYYY and MM look like, an original DOB, and what the final result would look like after subtracting them.

Cruise
Ammonite | Level 13
just edited my original post as per your suggestion
Astounding
PROC Star

If I have the question right (and I'm not really sure about that), you are starting with A and looking to calculate B and C.

 

Aiming to come up:

 

If that's the case, the formulas are straightforward (and now corrected):

 

B = int(A/100);

C = mod(A, 100);

 

Somehow I get the feeling that I'm leaving out a key piece of the question though.  I can't draw the link between this table and your original table.

Cruise
Ammonite | Level 13

Hi Respected,

I am starting with below. Date is truncated for confidentiality concern. So I have this 6 digit SAS date.

respected ad.png

I tried your code as:

year=substr(left(dob),1,4);
month=substr(left(dob),5,2);
B = int(year/100);
C = mod(year, 100);

That is resulted in:

respected advisor.png

 

Any alternatives?

Thanks

 

Astounding
PROC Star

Just mixing and matching names ... should be:

 

year = int(dob/100);

month = mod(dob, 100);

Cruise
Ammonite | Level 13
I still have 1890, 1898 sort of years after this.
Tom
Super User Tom
Super User

If you are getting DOB in the 1800's then either you have some really old people or the process that created that field did something wrong.  

 

Most likely it assumed the wrong century for a date string with a two digit year.  If you cannot correct that then you will need to do the best you can to fix the DOB after the fact.  If you have other information that could lead you believe that the 1890 is unreasonable but that 1990 is reasonable for that person then just add 100 years to the DOB.

 

Reeza
Super User

@Cruise Look at the first picture you posted. It looks like 1890. If it's not supposed to be 1890 what is it supposed to be. According to what I see, the code looks like it ran correctly. Either

 

1. Your dates are in some other format than you've specified. Did you import this data from Excel, from a date field

2. Your dates are wrong

3. Dates are correct and you have some really old people. 

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
  • 15 replies
  • 14872 views
  • 6 likes
  • 5 in conversation