Data step- If then else do

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Data step- If then else do

Hello Please guide,

I am using this step but I could not see in the output table

In the output--It is blank for Desc since it is a Char and '.' period for Week.


data dm2;

set dm1;

If tdate=>'03-Jan-2013'd and tdate<='09-Jan-2013'd then do;  Week=1;  Desc='Week: 1  (03-Jan-2013 - 09-Jan-2013)'; end; else

If tdate=>'10-Jan-2013'd and tdate<='16-Jan-2013'd then do;  Week=2;  Desc='Week: 2  (10-Jan-2013 - 16-Jan-2013)'; end; else

If tdate=>'17-Jan-2013'd and tdate<='23-Jan-2013'd then do;  Week=3;  Desc='Week: 3  (17-Jan-2013 - 23-Jan-2013)'; end;

run;

Thank you

Dipu


Accepted Solutions
Solution
‎02-10-2014 11:38 AM
Super User
Posts: 10,500

Re: Data step- If then else do

If the variable is datetime, then you want to use either a datetime literal OR use the date portion of the datetime variable.

Such as

if  '03JAN2013'd <= datepart(tdate) <= '09JAN2013'd then do; ...

View solution in original post


All Replies
Super User
Posts: 10,500

Re: Data step- If then else do

Try '03JAN2013'd , no hyphens in the date literals.

BTW SAS allows constructs like '03JAN2013'd <= tdate <= '09JAN2013'd.

Contributor
Posts: 43

Re: Data step- If then else do

Thanks Ballardw,

But it is still showing the same problem.

and the 'tdate' is in datetime format, will this help?

And I can use the other construct as you mentioned.

Thank you

Super User
Super User
Posts: 6,500

Re: Data step- If then else do

Use datetime literals if the value is datetime instead of date.

'03JAN2013:00:00'dt


Contributor
Posts: 43

Re: Data step- If then else do

Thank you To Ballrdw. I think that is where the problem lies.

Thank you Tom, Nice to see you after long time for my query

Thank you again.

:-)

Solution
‎02-10-2014 11:38 AM
Super User
Posts: 10,500

Re: Data step- If then else do

If the variable is datetime, then you want to use either a datetime literal OR use the date portion of the datetime variable.

Such as

if  '03JAN2013'd <= datepart(tdate) <= '09JAN2013'd then do; ...

Super Contributor
Posts: 644

Re: Data step- If then else do

@ballardw

SAS in recent versions does allow hyphens in date9 or date11 informats.  Hence

    if  '03-JAN-2013'd <= datepart(tdate) <= '09-JAN-2013'd then do;

should work

Richard in the land of the Long White Cloud

Contributor
Posts: 43

Re: Data step- If then else do

Thank you Richard,

I just tried with hyphen too and it did work.

Thanks

Dipu

☑ This topic is SOLVED.

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

Discussion stats
  • 7 replies
  • 361 views
  • 11 likes
  • 4 in conversation