Help using Base SAS procedures

IF-THEN Loop

Reply
N/A
Posts: 0

IF-THEN Loop

Hi,

I use the code as -

data Week;
set Day;
IF ("1/01/2001"<=Date<="7/01/2001") then 'Week'= 'Week1';
IF ("8/01/2001"<=Date<="14/01/2001") then 'Week' = 'Week2';
IF ("15/01/2001"<=Date<="21/01/2001") then 'Week' = 'Week3';
ELSE IF ("22/01/2001"<=Date<="28/01/2001")then 'Week' = 'Week4';
run;

The date is character in the code .

LOG ERROR 180-322: Statement is not valid or it is used out of proper order.


Kindly suggest where am wrong or any articles that deal with this coding...

Regards,
Mark
SAS Super FREQ
Posts: 8,868

Re: IF-THEN Loop

Posted in reply to deleted_user
Hi:
As I explained in a previous post, when you have syntax constructions like < or > or <= , then you must follow the guidelines laid out in the forum posting to which you were previously referred (on July 12 and shown below)

In that forum posting, it explains that you need to REPLACE < with &lt; in your code or <= with &lt;= or with LE.

Otherwise, your code will get truncated and no one will be able to help you. And, yes, it is sort of a pain to do this extra bit of work to post your code, but if you want folks to help you, it is a courtesy to pay attention to what people post and to show that you understand what they are trying to tell you.

For example, you might have wanted to show us something like this:
[pre]
data Week;
set Day;
IF ("1/01/2001" <= Date <= "7/01/2001") then Week= 'Week1';
...more code...
[/pre]

but if you had <= in your code the < would have messed up the forum posting. (And the reason it didn't mess up my code is that I followed the suggestion in the forum topic on preventing special characters from truncating your posting.)

If, for example, you tried an IF statement like this, there is an automatic problem if DATE is a SAS date variable. SAS expects any comparisons to a DATE variable to be done either as the numeric value or as the DATE7 or DATE9 version of the date like this (showing an equality test...not a "between" test):

[pre]
IF DATE = '01JAN2001'd then ...;
or
IF DATE = 14976 then ....;
[/pre]

Previous forum postings on the subject of date and date/time variables:
http://support.sas.com/forums/message.jspa?messageID=39424#39424
http://support.sas.com/forums/thread.jspa?messageID=33006胮

Previous forum postings on how to post code with special characters such as < and >
http://support.sas.com/forums/thread.jspa?messageID=27609 (someone already sent you this posting to read on July 12)
http://support.sas.com/forums/message.jspa?messageID=39361#39361

cynthia
Ask a Question
Discussion stats
  • 1 reply
  • 128 views
  • 0 likes
  • 2 in conversation