BookmarkSubscribeRSS Feed
u787bruw
Calcite | Level 5
* Create dataset;
data test;
	input var1 $10.;
	datalines;
	2023324
	2023324
	2023324
	2023324
	2023324
	20231230
	;
run;

* Alter data;
data test;
	set test;
	/* Modify timestamp format for 'CREATE_TIMESTAMP' */
	YEAR	= SUBSTR(var1, 1, 4);
	MONTH 	= SUBSTR(var1, 5, 1);
	DAY 	= SUBSTR(var1, 6, 2);
	FORM_CREATE_TIMESTAMP = CATS(YEAR, '-', MONTH, '-', DAY);
	DATE_CREATE_TIMESTAMP = INPUT(FORM_CREATE_TIMESTAMP, YYMMDD9.);
	FORMAT DATE_CREATE_TIMESTAMP YYMMDD10.;
run;

I have a column in a SAS dataset that is a 'character-type' and the values look something like '2020213'. All the values appear in the format, YYYYMDD, so I am concerned that if I convert the column like I did in the code above that it will screw up the values that have double-digit months or single-digit days (which it did) in my test above. How can I convert the values so that they look something more like:
'2022320' --> '2022-03-20'

'2022123' --> '2022-12-3'

 

I have fiddled around with doing something like this:

CHAR_DATE_STRIPPED = STRIP(CHAR_DATE);
DATE1 = INPUT(CHAR_DATE_STRIPPED, YYMMDD8.);
FORMAT DATE1 YYMMDD9.;

But I have gotten zero luck. The values are coming out as "."

12 REPLIES 12
Reeza
Super User
You cannot with any certainty.

Is 2023111
January 11, 2023 or November 1, 2023?
u787bruw
Calcite | Level 5

That would be January 11, 2023. The format of the values are 'year-month-day'.

Tom
Super User Tom
Super User

Why did you decide to divide the three ones into 11/1 instead of 1/11 ?

u787bruw
Calcite | Level 5

I am sorry, but I do not quite understand your question. Hopefully I can try to answer it, but the data I am working on is not created by me. The example I just gave above mimics the data I am working on

Reeza
Super User

So the days have leading zeroes?

 

How would Nov 1, 2023 be in the data?

Tom
Super User Tom
Super User

How do you know if a string like 2022123 should be treated as 2022-1-23 or 2022-12-3.  Either meaning is possible. 

 

Do you have extra information that can help you make a choice?  For example do you know the day of the month should never be more than one digit?  Or is there some other date field you can use to compate it to?

 

Where did these strings come from?  What process was used to create them?  Can you get at the original data instead?

u787bruw
Calcite | Level 5

Ah okay, now I understand the question. I know the data is treated as year-month-day because it is in the same sort of format as other columns that are numeric-type, meaning that they are 'YYYY-MM-DD'. I would like to just copy those columns into this, but the problem arises if the the data is not exactly the same. The dates could be one-day off for example.

PaigeMiller
Diamond | Level 26

@u787bruw wrote:

Ah okay, now I understand the question. I know the data is treated as year-month-day because it is in the same sort of format as other columns that are numeric-type, meaning that they are 'YYYY-MM-DD'. I would like to just copy those columns into this, but the problem arises if the the data is not exactly the same. The dates could be one-day off for example.


@u787bruw As far as I can see, you have not answered @Tom 's question. It seems he doesn't understand the meaning of 2022123, nor do I, and your reply doesn't shed any additional light on the matter. What is the date associated with 2022123?

--
Paige Miller
Tom
Super User Tom
Super User

@u787bruw wrote:

Ah okay, now I understand the question. I know the data is treated as year-month-day because it is in the same sort of format as other columns that are numeric-type, meaning that they are 'YYYY-MM-DD'. I would like to just copy those columns into this, but the problem arises if the the data is not exactly the same. The dates could be one-day off for example.


Copy?

 

If you want to convert a string of digits in the style YYYYMMDD into a DATE you can use the YYMMDD informat. Once you have date value you can use any of the many date formats to have the number display in a way that humans will recognize as a date.

datevar=input(stringvar,yymmdd10.);
format datevar yymmdd10.;

But if the digits strings do not represent an actual date, 20223456 for example, ; or are ambiguous about what date they represnet, 2022123, for example, then you need more information to convert them.

 

 

 

Reeza
Super User
Not one day off, months off if you cannot differentiate between the days/months.
ghosh
Barite | Level 11

Your character input values are not consistent, they range between lengths 7 and 8.  so it they were the same you could use:

data test;
infile datalines;
input var1 $;
	ymd=input(var1,yymmdd8.);
format ymd yymmdd10.;

datalines;
20230324
20231124
20231024
20230924
20230624
20231230
;
proc print;
run;

ghosh_0-1680708062907.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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