BookmarkSubscribeRSS Feed
heansy
Fluorite | Level 6

obs                     A                                               B         

1          2015-10-30T17:47:00             2015-10-30T17:47:00 

2          17OCT2016                                           17OCT2016

 

A is a text variable. I want to create B variable to let both partial and non-partial datetime to be in iso8601 format and B should be a numerical variable. Is that possible. Please help! Thanks in advance!

5 REPLIES 5
kiranv_
Rhodochrosite | Level 12

In case of text columns, everything is same, as values are text. But you cannot put different datatypes/formats in a single column for others, as that goes against basic principle of column.

 

Please check the below link for some explanation on this.

 

https://www.sqa.org.uk/e-learning/SoftDevRDS03CD/page_06.htm

 

Jagadishkatam
Amethyst | Level 16

I agree with @kiranv_, we could read the data at different rows with the below code but to write it in numeric formats different across different rows in the same column is something not sure if its possible. That too presenting the data in IS8601 format in numeric is not possible.

 

 

data have;
input A$20. ;
if prxmatch('m/\d{4}\-\d{2}\-\d{2}\w\d+/',a) then B=input(a,e8601dt.);
else B=input(a,date9.);
cards;
2015-10-30T17:47:00  
17OCT2016           
;

 

Thanks,
Jag
heansy
Fluorite | Level 6

Thank you! I try your code and it is really helpfull. If I want to present data in this way as below, is it possible?

 

obs                     A                                               B         

1          2015-10-30T17:47:00             2015-10-30T17:47:00 

2          2015-10-30                                              2015-10-30 

 

B is still numerical and for obs 2nd, it only contains date part but it looks like is8601dt format.

I think what I want is a numerical format which can present partial and non-partial info and it should look like ios8601 format. Thanks again!

 

 

data have;
input A $20. ;
if prxmatch('m/\d{4}\-\d{2}\-\d{2}\w\d+/',a) then B=input(a,e8601dt.);
else B=input(a,is8601da.);
cards;
2015-10-30T17:47:00
2015-10-30
;

Jagadishkatam
Amethyst | Level 16
Yes the data is getting converted to numeric we are able to read the data with different dates in same column but the issue is we cannot apply different formats to the same column(Numeric).





Thanks,
Jag
Tom
Super User Tom
Super User

You cannot change the format attached to a variable from observation to obsevation any more than you could change the name of the variable from observation to observation.

 

If you wanted to record the fact that for the second observation the time part is unknown then you need to store that information into another variable.  One way you could do that would be by storing the format that you want to use to display it.  Then at some point in the future you could use the PUTN() function to generate the strings that you see in column B.

 

Or why not store the date and the time into date and time variables instead of combining them into a singel datetime variable?  You could then very easily make a user defined format for the time that will display zero as blanks.

 

You might also be able to create a user written format, most likely requiring a user written function, that will display the datetime value differently when the timepart is zero. You could then attach that new format to your numeric variable.

http://support.sas.com/documentation/cdl/en/proc/70377/HTML/default/viewer.htm#n1eyyzaux0ze5ln1k03gl...

 

 

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
  • 5 replies
  • 843 views
  • 0 likes
  • 4 in conversation