Hi,
I'm generating samples and I'm trying to change a binary value in one particular column of the generated data. The data is sorted from large to small values in the T column and the binary values (BinT) should be 1 for values of T larger than the first T value where BinT=1.
So, the following dataset
Obs | Id | T | binT |
1 | 100 | 46 | 0 |
2 | 34 | 39 | 0 |
3 | 64 | 38 | 1 |
4 | 1 | 36 | 0 |
5 | 57 | 36 | 1 |
6 | 76 | 34 | 1 |
7 | 30 | 32 | 0 |
8 | 46 | 32 | 1 |
9 | 70 | 32 | 0 |
10 | 53 | 30 | 0 |
should look like
Obs | Id | T | binT |
1 | 100 | 46 | 1 |
2 | 34 | 39 | 1 |
3 | 64 | 38 | 1 |
4 | 1 | 36 | 0 |
5 | 57 | 36 | 1 |
6 | 76 | 34 | 1 |
7 | 30 | 32 | 0 |
8 | 46 | 32 | 1 |
9 | 70 | 32 | 0 |
10 | 53 | 30 | 0 |
I have tried several things, such as
data want;
_binT1=binT1;
do until(done);
set have;
done= (binT1=1);
if ^done then _binT1=1;
end;
output;
run;
and
data test;
x=binT1;
do _N_=1;
set treat_test;
if binT1=1 then leave;
else x=1;
end;
run;
but that does lead to the desired result.
Hoping that someone can point me into the right direction.
Best.
Jo
Here you go. It isn't extremely efficient, because you could theoretically have to process the entire data set twice (if your first binT=1 value is last) but for small-ish data sets it should get the job done. The idea is to first loop through the data and identify where the first binT=1 value appears. LEAVE-ing the loop immediately sets the value of i to the record where this occurs. Next, just loop through all the data and change the value of binT for those early records.
data want;
if 0 then set have nobs = _nobs;
do i = 1 to _nobs;
set have point = i;
if bint = 1 then leave;
end;
do j = 1 to _nobs;
set have point = j;
if j < i then bint = 1;
output;
end;
stop;
run;
Here you go. It isn't extremely efficient, because you could theoretically have to process the entire data set twice (if your first binT=1 value is last) but for small-ish data sets it should get the job done. The idea is to first loop through the data and identify where the first binT=1 value appears. LEAVE-ing the loop immediately sets the value of i to the record where this occurs. Next, just loop through all the data and change the value of binT for those early records.
data want;
if 0 then set have nobs = _nobs;
do i = 1 to _nobs;
set have point = i;
if bint = 1 then leave;
end;
do j = 1 to _nobs;
set have point = j;
if j < i then bint = 1;
output;
end;
stop;
run;
Works as a charm!
Thank you dugginsj.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.