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:
Obs | C03 | C06 | C08 | C05 | C07 | C02 | C01 | C04 | temp1 | temp2 | r1 | r2 |
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 |
But what I got like this(most of r2 values are missing).
Obs | C03 | C06 | C08 | C05 | C07 | C02 | C01 | C04 | temp1 | temp2 | r1 | r2 |
1 | 1 | 1589691 | 1385 | 6 | 4 | 74 | 98.564 | 5.247 | 0.8 | 0.2 | 60 | . |
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 | . | . |
6 | 1 | 784585 | 138 | 17 | 3 | 36 | 96.169 | 12.9356 | 0.5 | 0.4 | 57 | . |
7 | 0 | 5220856 | 611 | 7 | 5 | 53 | 120.258 | 8.8338 | 0.7 | 0.8 | 103 | . |
8 | E | 333766 | 456 | 5 | 4 | 60 | 134.078 | 5.308 | 0.1 | 0.2 | 12 | . |
9 | C | 432310 | 525 | 5 | 3 | 61 | 129.407 | 7.8711 | 0.1 | 0.2 | 12 | . |
10 | C | 8509368 | 2390 | 13 | 6 | 56 | 141.523 | 3.8929 | 0.1 | 0.5 | 12 | 57 |
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.
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.
Hi RW9,
Thank you, you are right. Temp{i} variable cause the problem. Thank you.
Also thank you RIchard.
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
Thank you Richard, however, it does not work for me. Also .A, .B, .C etc are already predefined in the data.
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
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.
or if Round(temp,0.1) =
as long as you are comparing nearest tenth.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.