Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Same values in all records as a result of multiple conditioning code

Status
Not open for further replies.

iren

Technical User
Mar 8, 2005
106
US
Hello,

I run the following code and in the result dataset baselineT1 I got the same value 2000 in each record under newfield ...…regardless of the multiple conditions below.

Surv_new is numeric data type .
Newfield is a character data type.

I have beaten my head but I do not have any idea why I get the same value. Any help would be greatly appreciated


data baselineT1;
set baselineT;
length newfield $6;
If 01/01/00 le surv_new le 12/31/00 then newfield ='2000 ';
Else if 01/01/01 le surv_new le 30/06/01 then newfield ='2001_1';
Else if 01/07/01 le surv_new le 12/31/01 then newfield ='2001_2';
Else if 01/01/02 le surv_new le 06/30/02 then newfield ='2002_1';
Else if 01/07/02 le surv_new le 12/31/02 then newfield ='2002_2';
Else if 01/01/03 le surv_new le 06/30/03 then newfield ='2003_1';
Else if 07/01/03 le surv_new le 12/31/03 then newfield ='2003_2';
Else if 01/01/04 le surv_new le 06/30/04 then newfield ='2004_1';
Else if 07/01/04 le surv_new le 12/31/04 then newfield ='2004_2';
Else if 01/01/05 le surv_new le 06/30/05 then newfield ='2005_1';
Else if 07/01/05 le surv_new le 12/31/05 then newfield ='2005_2';
run;
 
Your date formats are probably incorrect. I don't think SAS will recognise 01/01/00 as a date, because you've not specified that it IS a date.

Try this instead:-
Code:
data baselineT1;
  set  baselineT;
  length newfield $6;

  If       '01JAN2000'd le surv_new le '31DEC2000'd then newfield ='2000  ';
  Else if  '01JAN2001'd le surv_new le '30JUN2001'd then newfield ='2001_1';
  Else if ...
  ...
  ...
run;

Also, there is a typo in your dates list. The second date in the second line of the IF statements has the date and month the other way around to the rest of the records.

As a suggestion, I would also add in a final "else" statement to pick up anything greater than the final date range, even if there shouldn't be any, just in case.
 
In fact, having converted your code to run against a SASHELP table to view the results, the log revealed the problem immediately.
Code:
19     If       01/01/00 le date le 12/31/00 then newfield ='2000  ';
NOTE: Division by zero detected during the compilation phase, detected at line 19 column 17.
NOTE: Division by zero detected during the compilation phase, detected at line 19 column 37.

It interprets 01/01/00 as 1 dvided by 1 divided by 0. It doesn't recognise them as dates at all, just numbers.
 
Chris,

I tried that but newfield =xxx (else option). I did Proc contents for baselineT and it appears that surv_new is still a character! What is wrong?

data baselineT1;
set baselineT;
length newfield $6;
If '01JAN00'd le surv_new le '31DEC00'd then newfield ='2000 ';
Else if '01JAN01'd le surv_new le '30JUN01'd then newfield ='2001_1';
Else if '01JUL01'd le surv_new le '31DEC01'd then newfield = '2001_2';
Else if '01JAN02'd le surv_new le '30JUN02'd then newfield = '2002_1';
Else if '01JUL02'd le surv_new le '31DEC02'd then newfield = '2002_2';
Else if '01JAN03'd le surv_new le '30JUN03'd then newfield = '2003_1';
Else if '01JUL03'd le surv_new le '31DEC03'd then newfield = '2003_2';
Else if '01JAN04'd le surv_new le '30JUN04'd then newfield = '2004_1';
Else if '01JUL04'd le surv_new le '31DEC04'd then newfield = '2004_2';
Else if '01JAN05'd le surv_new le '30JUN05'd then newfield = '2005_1';
Else if '01JUL05'd le surv_new le'31DEC05'd then newfield = '2005_2';
else newfield ='xxx';
run;
 
surv_new is a character? Surv_new will need to be a date (numeric) field in order for this to work. What does surv_new look like? If surv_new is not a SAS date, then you'll need to use an INPUT function to convert it.
- Give an example of what the surv_new field looks like.
- Include the proc contents results for surv_new
- paste the relevant part of the log file into this post.

That should give us enough info to fix the problem.
 
Irin,

Here is a way you can generate your text with fewer lines. I noticed that for year 2000 you dont want to add any text. I also noticed that you divided the year in half Jan to June. So here is code if the surv_new var is a numeric var (a sas date var).
Code:
data baselineT1;
set  baselineT;
  length newfield $6;
  if month(serv_new) le 6 and 
     day(serv_new) le 31 then 
     exttext='_1';
   else 
     exttext ='_2';
   *** Special exclusion for the year 2000 ***;

   if year(serv_new) eq 2000 then
     exttext =' '; 
   newfield = trim(left(put(year(serv_new),8.)))||exttext;
run;

If the surv_new var is a character variable ( a text date) Chris is right you would need a format to read that date text value as a SAS date value.
Code:
data baselineT1;
 set baselineT;
  length newfield $6;
  if month(input(serv_new,mmddyy8.)) le 6 and 
     day(input(serv_new,mmddyy8.))  le 31 then 
     exttext='_1';
   else 
     exttext ='_2';
   *** Special exclusion for the year 2000 ***;

   if year(input(serv_new,mmddyy8.))  eq 2000 then
     exttext =' '; 
   newfield = trim(left(put(input(serv_new,mmddyy8.) ,8.)))||exttext;
run;

I noticed that you don't save that much in line space.
I hope that this has helped you,
Klaz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top