BookmarkSubscribeRSS Feed
usuario_estudo
Calcite | Level 5

I'd like to write a code that creates column C from A and B. Column C will be, the value of A from the row where B is the smallest value greater or equal to 0.99. If there are repeated values as in the example, the value must be the first of them.

 

A     B             C

1     0.2           4

2     0.05         4

3     0.9           4

4     0.9901     4

5     0.9901     4

6     0.9901     4

7     0.995       4

2 REPLIES 2
Kurt_Bremser
Super User

Use a double do loop:

data want;
_b = 9999999999;
do until (done1);
  set have end=done1;
  if b > .99 and b < _b
  then do;
    _c = a;
    _b = b;
  end;
end;
do until (done2);
  set have end=done2;
  c = _c;
  output;
end;
drop _:;
run;
ghosh
Barite | Level 11
data have;
	input A B;
	cards;
1     0.2    
2     0.05   
3     0.9    
4     0.9901 
5     0.9901 
6     0.9901 
7     0.995 
;

proc sql noprint;
	select a, min(b) into :a trimmed, :b trimmed 
	  from have 
where b ge 0.99; quit; %put a=&a b=&b; data want; set have; c=&a; run;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 865 views
  • 0 likes
  • 3 in conversation