BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
JoVe
Calcite | Level 5

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 

ObsIdTbinT
1100460
234390
364381
41360
557361
676341
730320
846321
970320
1053300

should look like

ObsIdTbinT
1100461
234391
364381
41360
557361
676341
730320
846321
970320
1053300

 

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

1 ACCEPTED SOLUTION

Accepted Solutions
Duggins
Obsidian | Level 7

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;

View solution in original post

2 REPLIES 2
Duggins
Obsidian | Level 7

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;
JoVe
Calcite | Level 5

Works as a charm!

 

Thank you dugginsj. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to connect to databases in SAS Viya

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.

Discussion stats
  • 2 replies
  • 664 views
  • 2 likes
  • 2 in conversation