Multiple do loops in a data do not return what was expected

Reply
Occasional Contributor
Posts: 16

Multiple do loops in a data do not return what was expected

Hi All,

I need to use the combination conditions of two variables: temp1 or temp2 and another eight variables: c01-c08 to define the value of r1 and r2. However, I did not get what I wanted.

The data and results should look like this:

ObsC03C06C08C05C07C02C01C04temp1temp2r1r2
1115896911385647498.5645.2470.80.260103
218316974454591112.116.05430.20.7103103
3C43828717015567142.05112.35610.10.51257
445134241513426145.34512.17830.10.31267
516327875493371110.2747.90740.20.81032
617845851381733696.16912.93560.50.45757
7052208566117553120.2588.83380.70.81032
8E3337664565460134.0785.3080.10.212103
9C4323105255361129.4077.87110.10.212103
10C8509368239013656141.5233.89290.10.51257

But what I got like this(most of r2 values are missing).

ObsC03C06C08C05C07C02C01C04temp1temp2r1r2
1115896911385647498.5645.2470.80.260.
218316974454591112.116.05430.20.7103103
3C43828717015567142.05112.35610.10.51257
445134241513426145.34512.17830.10.31267
516327875493371110.2747.90740.20.8..
617845851381733696.16912.93560.50.457.
7052208566117553120.2588.83380.70.8103.
8E3337664565460134.0785.3080.10.212.
9C4323105255361129.4077.87110.10.212.
10C8509368239013656141.5233.89290.10.51257

Below is my SAS code:

data temp2;
set temp1;
array temp {2} temp1-temp2;;
array r{2} r1-r2;
do i=1 to 2;

  if temp{i}=0.1 then do;
   if C01>=0 then r{i}=12;
   else r{i}=33;
  end;

  if temp{i}=0.2 then do;
   if C02 in (.B,.D,.G) then r{i}=27;
   else if C02 >=0 then r{i}=103;
   else r{i}=109;
  end;

  if temp{i}=0.3 then do;
   if C03=.C then r{i}=27;
   else if C03 =.E then r{i}=.;
   else if C03>=1 then r{i}=67;
   else r{i}=2;
  end;

  if temp{i}=0.4 then do;
   if C04>=0 then r{i}=39;
   else r{i}=33;
  end;

  if temp{i}=0.5 then do;
   if C05 =.A then r{i}=.;
   else if C05>=0 then r{i}=57;
   else r{i}=2;
  end;

  if temp{i}=0.6 then do;
   if C06>=0  then r{i}=2;
   else r{i}=109;
  end;

  if temp{i}=0.7 then do;
   if C07 in (.C,.D) then r{i}=27;
   else if C07>=0 then r{i}=103;
   else r{i}=2;
  end;

  if temp{i}=0.8 then do;
   if C08=.B then r{i}=27;
   else if C08>=0 then r{i}=60;
   else r{i}=109;
  end;
end;
drop i;
run;

proc print data=temp2;run;

Anyone knows what's wrong with my code? Or how to fix it?  Thank you so much.

Super User
Super User
Posts: 7,727

Re: Multiple do loops in a data do not return what was expected

Well, at a guess, as I am not typing in all that test data, I would say that your TEMPx value doesn't resolve in any of the if statements, hence it would be missing.  This could be for a number of reasons, the first I would check is that your values are *exactly* 0.1 etc.  Sometimes SAS hides fractional parts.  So change your if statemetns to a range e.g:

if 0.70 < temp{i} <= 0.8 then do;

The next bit to check is the second if statement doing this:

if C02 in (.B,.D,.G) then r{i}=27;

What is .B etc?

Maybe provide some test data (in the form of a datastep with datalines) as there is no clear indication of what formats Rx/Tempx are.

Occasional Contributor
Posts: 16

Re: Multiple do loops in a data do not return what was expected

Hi RW9,

Thank you, you are right. Temp{i} variable cause the problem. Thank you.

Also thank you RIchard.


Super Contributor
Posts: 644

Re: Multiple do loops in a data do not return what was expected

The missing statement is missing!  Your code works unchanged following

data temp1 ;

  missing A B C D E F G ;

  infile datalines missover dlm = '09'x ;

  input Obs C03 C06 C08 C05 C07 C02 C01 C04 temp1 temp2 r1 r2 ;

  drop obs r1 r2 ;

datalines ;

1 1 1589691 1385 6 4 74 98.564 5.247 0.8 0.2 60 103

2 1 831697 445 4 5 91 112.11 6.0543 0.2 0.7 103 103

3 C 438287 170 15 5 67 142.051 12.3561 0.1 0.5 12 57

4 4 513424 151 3 4 26 145.345 12.1783 0.1 0.3 12 67

5 1 632787 549 3 3 71 110.274 7.9074 0.2 0.8 103 2

6 1 784585 138 17 3 36 96.169 12.9356 0.5 0.4 57 57

7 0 5220856 611 7 5 53 120.258 8.8338 0.7 0.8 103 2

8 E 333766 456 5 4 60 134.078 5.308 0.1 0.2 12 103

9 C 432310 525 5 3 61 129.407 7.8711 0.1 0.2 12 103

10 C 8509368 2390 13 6 56 141.523 3.8929 0.1 0.5 12 57

;

Result

Obs

C03

C06

C08

C05

C07

C02

C01

C04

temp1

temp2

r1

r2

1

1

1589691

1385

6

4

74

98.564

5.2470

0.8

0.2

60

103

2

1

831697

445

4

5

91

112.110

6.0543

0.2

0.7

103

103

3

C

438287

170

15

5

67

142.051

12.3561

0.1

0.5

12

57

4

4

513424

151

3

4

26

145.345

12.1783

0.1

0.3

12

67

5

1

632787

549

3

3

71

110.274

7.9074

0.2

0.8

103

60

6

1

784585

138

17

3

36

96.169

12.9356

0.5

0.4

57

39

7

0

5220856

611

7

5

53

120.258

8.8338

0.7

0.8

103

60

8

E

333766

456

5

4

60

134.078

5.3080

0.1

0.2

12

103

9

C

432310

525

5

3

61

129.407

7.8711

0.1

0.2

12

103

10

C

8509368

2390

13

6

56

141.523

3.8929

0.1

0.5

12

57

Richard

Occasional Contributor
Posts: 16

Re: Multiple do loops in a data do not return what was expected

Thank you Richard, however, it does not work for me. Also .A, .B, .C etc are already predefined in the data.

Super Contributor
Posts: 644

Re: Multiple do loops in a data do not return what was expected

Try running your code but including the missing statement after the data statement.  "missing" is a global statement that notifies SAS that your code handles special missing values.

Richard

Occasional Contributor
Posts: 16

Re: Multiple do loops in a data do not return what was expected

HI Richard,

Thank you so much for your help. The problem was solved. The variable temp{I} is a floating number, so temp=0.1 do not return anything, i should use 0.009<temp(I)<0.101.

Super User
Posts: 11,139

Re: Multiple do loops in a data do not return what was expected

or if  Round(temp,0.1) =

as long as you are comparing nearest tenth.

Ask a Question
Discussion stats
  • 7 replies
  • 290 views
  • 0 likes
  • 4 in conversation