Question;
data sv;
input visit$;
datalines;
cycle1
cycle2
unsch
unsch
cycle3
unsch
unsch
cycle4
cycle5
;
run;
Expecting;
visit Number
------ ------
cycle1 1
cycle2 2
unsch 2.1
unsch 2.2
cycle3 3
unsch 3.1
unsch 3.2
cycle4 4
cycle5 5
___________________________________
Code I used(but not giving expected result)
data visit;
set sv;
if visit in ("cycle") then seq=1;
else seq+1;
retain seq;
if visit in("unsch") then seq=seq+3;
run;
data visit;
set sv;
retain cy 0 subcy;
if find(visit,'cycle')
then do;
cy + 1;
subcy = 0;
end;
else subcy + 1;
number = catx(".",cy,ifc(subcy,put(subcy,best.),""));
keep visit number;
run;
Untested, posted from my tablet.
IN allows you to avoid multiple OR conditions, it doesn't check for portion of a string. You want FIND, INDEX, CONTAINS or the colon operator. =: will check if the strings start with the text cycle.
data visit;
set sv;
retain seq 0;
if visit =: ("cycle") then seq+1;
else seq + 0.1;
format seq 8.1;
run;
@Ajayvit wrote:
Question;
data sv;
input visit$;
datalines;
cycle1
cycle2
unsch
unsch
cycle3
unsch
unsch
cycle4
cycle5
;
run;
Expecting;
visit Number
------ ------
cycle1 1
cycle2 2
unsch 2.1
unsch 2.2
cycle3 3
unsch 3.1
unsch 3.2
cycle4 4
cycle5 5
___________________________________Code I used(but not giving expected result)
data visit;
set sv;
if visit in ("cycle") then seq=1;
else seq+1;
retain seq;
if visit in("unsch") then seq=seq+3;
run;
data visit;
set sv;
retain cy 0 subcy;
if find(visit,'cycle')
then do;
cy + 1;
subcy = 0;
end;
else subcy + 1;
number = catx(".",cy,ifc(subcy,put(subcy,best.),""));
keep visit number;
run;
Untested, posted from my tablet.
Dear Kurt,
Thank you so much for your kind help.
Regards,
Ajay
Hi Kurt,
I took the reference to your code and did some modifications.
This is also giving the same result.
data sv_1;
set sv;
if find(visit,"cycle") then do;
sq+1;
nd=0;
end;
else nd+1;
seq=catx(".",sq,nd);
keep visit seq;
run;
I know you have a working solution now, but the problem struck me as much simpler:
data sv_1;
set sv;
if visit = 'unsch' then seq + 0.1;
else seq = int(1 + seq);
run;
Of course, all solutions run into trouble if there are 10 unscheduled visits in a row.
@Astounding wrote:
Of course, all solutions run into trouble if there are 10 unscheduled visits in a row.
Mine won't, as I build the result string from two integers, so 1.10 is followed by 1.11.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.