Help using Base SAS procedures

Date format from yyyy-mm-dd to yyyymmdd

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Date format from yyyy-mm-dd to yyyymmdd

I want to get all the dates that are less than 20170701 but my date format is in yyyy-mm-dd and so it's a character

 

input(tc.valid_from,10.) < 20170701

this code gives me this error message: "An error occured executing the workspace job "Report1". The server is disconnected." if I comment this section out then the project runs fine.

 

help?

 

I'm using proc sql and I'd prefer to stay with it but I'll listen to any suggestions! Smiley Happy


Accepted Solutions
Solution
‎08-17-2017 01:01 PM
Occasional Contributor
Posts: 14

Re: Date format from yyyy-mm-dd to yyyymmdd

Posted in reply to Astounding

Thank you both. I ended up using this 

tc.valid_from < '01JUL2017'

View solution in original post


All Replies
Super User
Posts: 23,776

Re: Date format from yyyy-mm-dd to yyyymmdd

Posted in reply to serrld113

Are you sure it's a character? If so, use the yymmdd INFORMAT, NOT the 10 format. INPUT uses the format it currently appears in. 

 

You should create a SAS date, IMO.

 

input(tc.valid_from, yymmdd8.) < '01Jul2017'd 
Occasional Contributor
Posts: 14

Re: Date format from yyyy-mm-dd to yyyymmdd

[ Edited ]

yes, I know it's a character. When I hover over the variable name on the table, there's this little gray "cloud" that describes the variable as such: Type:Character,Length:10,label:Valid_From

 

your suggested code returns error 22-322 Syntax error and error 76-322 Smiley Sad

Super User
Posts: 23,776

Re: Date format from yyyy-mm-dd to yyyymmdd

Posted in reply to serrld113

Then you have some other issue that you're not showing, my code is correct assuming your variable is character. 

 

data check;
length x $10.; format x $10.;
x="20170701";
y = input(x, yymmdd8.);
format y date9.;

if y < "01Jul2017"d then flag=1;
else flag=0;
run;

Post your full code AND log.

Super User
Posts: 6,785

Re: Date format from yyyy-mm-dd to yyyymmdd

Posted in reply to serrld113

It sounds like your existing variable is character, including dashes.  For that, try:

 

input(tc.valid_from, yymmdd10.) < '01Jul2017'd

 

You need to read 10 characters (not 8).

Solution
‎08-17-2017 01:01 PM
Occasional Contributor
Posts: 14

Re: Date format from yyyy-mm-dd to yyyymmdd

Posted in reply to Astounding

Thank you both. I ended up using this 

tc.valid_from < '01JUL2017'
Super User
Posts: 23,776

Re: Date format from yyyy-mm-dd to yyyymmdd

Posted in reply to serrld113

If this is a character variable as you state, you won't get errors but it won't work as you expect. It's a character comparison, not a numerical comparison. 

 


serrld113 wrote:

Thank you both. I ended up using this 

tc.valid_from < '01JUL2017'

 

 

Occasional Contributor
Posts: 14

Re: Date format from yyyy-mm-dd to yyyymmdd

@Reeza, I really appreciate your help. This is in fact a character as I state, and the results are all the dates that I expect. Thank you again for your contribution. 

Super User
Posts: 23,776

Re: Date format from yyyy-mm-dd to yyyymmdd

Posted in reply to serrld113

I'm putting this example for future users. The answer you've marked as correct cannot be correct given what you've stated that the variable is character, so most likely there's something else going on that you haven't stated. 

 

See the sample code below. 

 

*create sample data with format described by OP, date as character in format YYYY-MM-DD and a SAS date variable;
data sample;
do date='01Jan2017'd to '31Dec2017'd;
date_char = put(date, yymmdd10.);
output;
end;
run;

proc sql;
*Correct answer according to OP;
create table wrong as
select * 
from sample
where date_char < '01JUL2017';

*Comparison with date variables;
create table correct1 as
select *
from sample
where date < '01JUL2017'd;

create table correct2 as
select *
from sample
where input(date_char, yymmdd10.) < '01JUL2017'd;

quit;
Occasional Contributor
Posts: 14

Re: Date format from yyyy-mm-dd to yyyymmdd

Thank you again, @Reeza. You've been very helpful.

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 1978 views
  • 0 likes
  • 3 in conversation