DATA Step, Macro, Functions and more

A numerical variable contains both partial and non-partial iso8601 datetime

Reply
New Contributor
Posts: 4

A numerical variable contains both partial and non-partial iso8601 datetime

[ Edited ]

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!

PROC Star
Posts: 326

Re: A numerical variable contains both partial and non-partial iso8601 datetime

[ Edited ]

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

 

Trusted Advisor
Posts: 1,137

Re: A numerical variable contains both partial and non-partial iso8601 datetime

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
New Contributor
Posts: 4

Re: A numerical variable contains both partial and non-partial iso8601 datetime

Posted in reply to Jagadishkatam

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
;

Trusted Advisor
Posts: 1,137

Re: A numerical variable contains both partial and non-partial iso8601 datetime

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
Super User
Super User
Posts: 7,074

Re: A numerical variable contains both partial and non-partial iso8601 datetime

[ Edited ]

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...

 

 

Ask a Question
Discussion stats
  • 5 replies
  • 149 views
  • 0 likes
  • 4 in conversation