BookmarkSubscribeRSS Feed
GAL1986
Fluorite | Level 6

I am trying to create a unique ID that is a combination of an ID variable and a date. 

In both datasets I have an ID variable which is a numeric variable with a length of 8 and format Best12. In one dataset I have delivery_date which is length 8, format MMDDYY10. and informat date9. In the other I have new_delivery_date which has the same characteristics.

 

The code I am using is

ID2=put(ID, 8.);

del_date2=put(delivery_date, 9.);

Del_ID = strip(ID2)||strip(del_date2);

 

And

 

ID2=put(ID, 8.);

del_date2=put(new_del_date, 9.);

Del_ID = strip(ID2)||strip(del_date2);

 

Here is an example of the del_ID for one patient in both datasets. Do you have any idea why the Del_IDs would not match in the datasets if the variables are all formatted the same way?

 

Dataset1:

ID 1001958  Del_ID 100195819038  new_del_date 02/15/2012

Dataset 2: 

ID 1001958  Del_ID 100195819039  new_del_date 02/15/2012

 

Why does one Del_ID end in 8 and the other in 9 if the dates are formated the same way?

2 REPLIES 2
Tom
Super User Tom
Super User

SAS stores dates as the number of days.  All numeric variables in SAS are floating point values. The date formats (like MMDDYY , DDMMYY, YYMMDD, DATE, etc) will only use the integer part of the value.

But 9. format you used to convert the number of days to an digit string will round instead.

Example:

1    data test;
2      today=today()+0.6;
3      put today= +1 today date9. +1 today 9. ;
4    run;

today=22196.6  08OCT2020     22197

If the goal is to make a string of digits that represents ID and DELIVERY_DATE then you should use leading zeros in the ID value to force it to always use the same number of digits. To represent dates in strings use Y-M-D order so that the strings will sort properly.

 

Like this:

length DEL_ID $16;
DEL_ID = put(id,Z8.) || put(delivery_date,yymmdd8.);
GAL1986
Fluorite | Level 6
Thank you! I will try this today!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 866 views
  • 1 like
  • 2 in conversation