Dear friends,
I would like to create the "combo2 and old" at the highlight below based on the conditions, and comparing the variables at end of the condition or 1st record dose not apply the condition. I am wondering what is wrong with the "do while" statement.
Thank you very much!
data t;
input dt date9. name $ ;
format dt mmddyy10.;
datalines;
09SEP2015 a
29SEP2015 b
30SEP2015 a
06OCT2015 b
02NOV2015 c
16NOV2015 a
23NOV2015 c
30NOV2015 a
;
run;
data t1;
set t;
format dt1 mmddyy10. ;
retain dt1 combo1 combo2;
length combo1 combo2 $20. ;
if name = 'c' and dt ='02NOV2015'd then do;
combo1 = 'a+b';
combo2 = 'c';
dt1 = dt;
end;
else do;
do while (.< dt1 < dt < dt1 + 20);
if findw(combo2, strip(name)) = 0 then combo2 = cats(combo2,"+", strip(name));
end;
do i = 1 to countc(combo1, "+") + 1 ;
if findw(combo2, strip(scan(combo1, i, "+"))) > 0 then old = 1;
else old = 0;
end;
end;
run;
dt | name | Combo1 | combo2 | old | Dt1 |
9/9/2015 | a | ||||
9/29/2015 | b | ||||
9/30/2015 | a | ||||
10/6/2015 | b | a+b | |||
11/2/2015 | c | 11/2/2015 | |||
11/16/2015 | a | c+a | 1 | 11/2/2015 | |
11/23/2015 | c | 11/2/2015 | |||
11/30/2015 | a | 11/2/2015 |
I'm not really following the logic here. Can you elaborate?
Start here and see if this clears it up. This statement causes a problem:
combo2 = 'c';
It defines COMBO2 as being 1 character long, so there isn't room to add more characters to the end of it. Try adding this statement beforehand:
length combo2 $ 10;
Good thing it didn't run. If it did run, it would still be running for as long as you keep your computer turned on. The logic of the DO WHILE loop will never change your condition from being true to being false, so it will run forever.
Perhaps you should explain what it is you would like the DO WHILE loop to accomplish, and we can modify the program accordingly.
@Ivy wrote:
Dear friends,
I would like to create the "combo2 and old" at the highlight below based on the conditions, and comparing the variables at end of the condition or 1st record dose not apply the condition. I am wondering what is wrong with the "do while" statement.
Thank you very much!
data t;
input dt date9. name $ ;
format dt mmddyy10.;
datalines;
09SEP2015 a
29SEP2015 b
30SEP2015 a
06OCT2015 b
02NOV2015 c
16NOV2015 a
23NOV2015 c
30NOV2015 a
;
run;
data t1;
set t;
format dt1 mmddyy10. ;
retain dt1 combo1 combo2;length combo1 combo2 $20. ;
if name = 'c' and dt ='02NOV2015'd then do;
combo1 = 'a+b';
combo2 = 'c';
dt1 = dt;
end;
else do;
do while (dt1 < dt < dt1 + 20);
if findw(combo2, strip(name)) = 0 then combo2 = cats(combo2,"+", strip(name));
end;
do i = 1 to countc(combo1, "+") + 1 ;
if findw(combo2, strip(scan(combo1, i, "+"))) > 0 then old = 1;
else old = 0;
end;
end;
run;
dt name Combo1 combo2 old 9/9/2015 a 9/29/2015 b 9/30/2015 a 10/6/2015 b a+b 11/2/2015 c c 11/16/2015 a c+a 1 11/23/2015 c 11/30/2015 a
Since DT1 starts out = DT and never changes the comparison
(dt1 < dt < dt1 + 20)
is always false. For any value A<A is false. So the Do While loop never initiates because you have told it not to.
For that Do While loop to have any use you likely need to be doing something, but I haven't a clue what the need is, to the variable DT1 and your start condition needs to be better defined as well as your rule for how to determine which records need to be processed in general.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.