## Substr Syntax

Solved
Occasional Contributor
Posts: 18

# Substr Syntax

I'm using SAS Enterprise guide, and want to convert data from table A to Date10.  Why doesn't the code below work?  The Trm function looks fine...

Data A;

Input DT \$25.;

Datalines;

2012-07-07 00:00:00.00;

Run;

Data B;

Set A;

DaT = input(substr(strip(Dt),1,10),mmddyy10.);

trm = substr(strip(Dt),1,10);

Run;

I'm going crazy, please help!

Accepted Solutions
Solution
‎12-03-2014 11:13 AM
Trusted Advisor
Posts: 1,270

## Re: Substr Syntax

Please try this, just added datepart.

Data B;

Set A;

DaT = datepart(input(strip(Dt),anydtdtm.));

format DaT mmddyy10.;

trm = substr(strip(Dt),1,10);

Run;

All Replies
Trusted Advisor
Posts: 1,270

## Re: Substr Syntax

Try this.

Data B;

Set A;

DaT = input(substr(strip(Dt),1,10),anydtdte.);

format DaT mmddyy10.;

trm = substr(strip(Dt),1,10);

Run;

Occasional Contributor
Posts: 18

## Re: Substr Syntax

Hi Stat, I tried and only got a period (.) for DaT...

I tried changing anydtdte to anydtdtm and then got all astricks ********

Data B;

Set A;

DaT = input(substr(strip(Dt),1,10),anydtdtm.);

format DaT mmddyy10.;

trm = substr(strip(Dt),1,10);

Run;

Still stumped...

Solution
‎12-03-2014 11:13 AM
Trusted Advisor
Posts: 1,270

## Re: Substr Syntax

Please try this, just added datepart.

Data B;

Set A;

DaT = datepart(input(strip(Dt),anydtdtm.));

format DaT mmddyy10.;

trm = substr(strip(Dt),1,10);

Run;

Super Contributor
Posts: 1,636

## Re: Substr Syntax

Data A;

Input DT \$25.;

Datalines;

2012-07-07 00:00:00.00

;

Run;

Data B;

Set A;

DaT = input(substr(strip(Dt),1,10),yymmdd10.);

trm = substr(strip(Dt),1,10);

format Dat mmddyy10.;

Run;

proc print;run;

Obs              DT                              DaT                trm

1     2012-07-07 00:00:00.00    07/07/2012    2012-07-07

Super User
Posts: 6,753

## Re: Substr Syntax

One problem you are hitting is that you are using the mmddyy informat, when you data is actually in yymmdd form.

A second problem is that you added a semicolon on the line of raw data.

If you are actually starting from reading the raw data, you can do this easily:

Data A;

Input DT yymmdd10.;

format dt date9.;

Datalines;

2012-07-07 00:00:00.00

;

If you already have a character string (\$25 long) in DT, you can apply the same idea in a DATA step:

newvar = input(DT, yymmdd10.);

format newvar date9.;

Note that DATE9. is wide enough, although you could try DATE10. if you really wanted to.

Good luck.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 5 replies
• 325 views
• 6 likes
• 4 in conversation