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

Hi,

 

I'm having a tough time converting a couple of character variable fields into date fields. Each of the variables listed below are in the format of "2017-01-31" in character format. I tried to manually convert it but it's not working. Each of the _new variables I created only removed the "-" sign but still have blank spaces in them despite including the trim() function.

data new;
	set raw;

	Opened_dt_new = trim(tranwrd(opened_dt, "-", ""));
	Maturity_dt_new = trim(tranwrd(maturity_dt, "-", ""));
	Fico_dt_new = trim(tranwrd(fico_dt, "-", ""));
	Expiration_dt_new = trim(tranwrd(expiration_dt, "-", ""));

	drop opened_dt maturity_dt Fico_dt expiration_dt;

	Opened_dt = input(Opened_dt_new, YYMMDD10.);
	format Opened_dt YYMMDD10.;
	Maturity_dt = input(Maturity_dt_new, YYMMDD10.);
	format Maturity_dt YYMMDD10.;
	Fico_dt = input(Fico_dt_new, YYMMDD10.);
	format Fico_dt YYMMDD10.;
	Expiration_dt = input(Expiration_dt_new, YYMMDD10.);
	format Expiration_dt YYMMDD10.;

run;

Due to the fact that the _new variables have space in it, Format $YYMMDD was not found or could not be loaded. What is wrong here? 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

1. Use COMPRESS() to remove the - if you want

2. You cannot convert and save back to the same name, it has to have a new name. Yes it's annoying but it's the rules for now. You can rename your old variables before and then assign them the old name in the conversion is a common work around. 

3. You do not need to remove the - ahead of time, you can use INPUT directly on it. 

 

This is what you need to do, note that it looks the exact same but you have different variable types.

 

data demo;
date_have= '2017-01-31';

date_want = input(date_have, yymmdd10.);
format date_want yymmdd10.;


run;

*check your types and formats;
proc contents data=demo;
run;

 


@Bankshot wrote:

Hi,

 

I'm having a tough time converting a couple of character variable fields into date fields. Each of the variables listed below are in the format of "2017-01-31" in character format. I tried to manually convert it but it's not working. Each of the _new variables I created only removed the "-" sign but still have blank spaces in them despite including the trim() function.

data new;
	set raw;

	Opened_dt_new = trim(tranwrd(opened_dt, "-", ""));
	Maturity_dt_new = trim(tranwrd(maturity_dt, "-", ""));
	Fico_dt_new = trim(tranwrd(fico_dt, "-", ""));
	Expiration_dt_new = trim(tranwrd(expiration_dt, "-", ""));

	drop opened_dt maturity_dt Fico_dt expiration_dt;

	Opened_dt = input(Opened_dt_new, YYMMDD10.);
	format Opened_dt YYMMDD10.;
	Maturity_dt = input(Maturity_dt_new, YYMMDD10.);
	format Maturity_dt YYMMDD10.;
	Fico_dt = input(Fico_dt_new, YYMMDD10.);
	format Fico_dt YYMMDD10.;
	Expiration_dt = input(Expiration_dt_new, YYMMDD10.);
	format Expiration_dt YYMMDD10.;

run;

Due to the fact that the _new variables have space in it, Format $YYMMDD was not found or could not be loaded. What is wrong here? 



 

View solution in original post

4 REPLIES 4
Bankshot
Obsidian | Level 7

For further clarification, after the using the 

trim(tranwrd(opened_dt, "-", ""))

method, the output looks like 

opened_dt

2017 01 31;

 

where I'd like it to be:

20170131;

Reeza
Super User

1. Use COMPRESS() to remove the - if you want

2. You cannot convert and save back to the same name, it has to have a new name. Yes it's annoying but it's the rules for now. You can rename your old variables before and then assign them the old name in the conversion is a common work around. 

3. You do not need to remove the - ahead of time, you can use INPUT directly on it. 

 

This is what you need to do, note that it looks the exact same but you have different variable types.

 

data demo;
date_have= '2017-01-31';

date_want = input(date_have, yymmdd10.);
format date_want yymmdd10.;


run;

*check your types and formats;
proc contents data=demo;
run;

 


@Bankshot wrote:

Hi,

 

I'm having a tough time converting a couple of character variable fields into date fields. Each of the variables listed below are in the format of "2017-01-31" in character format. I tried to manually convert it but it's not working. Each of the _new variables I created only removed the "-" sign but still have blank spaces in them despite including the trim() function.

data new;
	set raw;

	Opened_dt_new = trim(tranwrd(opened_dt, "-", ""));
	Maturity_dt_new = trim(tranwrd(maturity_dt, "-", ""));
	Fico_dt_new = trim(tranwrd(fico_dt, "-", ""));
	Expiration_dt_new = trim(tranwrd(expiration_dt, "-", ""));

	drop opened_dt maturity_dt Fico_dt expiration_dt;

	Opened_dt = input(Opened_dt_new, YYMMDD10.);
	format Opened_dt YYMMDD10.;
	Maturity_dt = input(Maturity_dt_new, YYMMDD10.);
	format Maturity_dt YYMMDD10.;
	Fico_dt = input(Fico_dt_new, YYMMDD10.);
	format Fico_dt YYMMDD10.;
	Expiration_dt = input(Expiration_dt_new, YYMMDD10.);
	format Expiration_dt YYMMDD10.;

run;

Due to the fact that the _new variables have space in it, Format $YYMMDD was not found or could not be loaded. What is wrong here? 



 

Kurt_Bremser
Super User

Actually, if you want it to look exactly the same, there needs to be a slight change to the format:

format date_want yymmddd10.;

as the standard yymmdd format will use slashes instead of dashes.

 

Now that's almost poetry. Slashes/dashes 🙂

 

Bankshot
Obsidian | Level 7

"Slashes/dashes" I like that. You should trademark that

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
  • 4 replies
  • 1926 views
  • 3 likes
  • 3 in conversation