We have a SAS data set TEST
data test;
input Cust_ID $ visit $ visdt date7.;
format visdt date7.;
cards;
001 uns 01jun13
001 visit1 02jun13
001 visit2 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit3 06jun13
001 uns 07jun13
001 uns 08jun13
001 uns 01jun13
001 visit4 02jun13
001 visit5 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit6 06jun13
001 uns 07jun13
001 uns 08jun13
;
run;
We want the output like:
Cust_ID Vist visdt Visitseq
001 uns 01JUN13 0.1
001 visit1 02JUN13 1.0
001 visit2 03JUN13 2.0
001 uns 04JUN13 2.1
001 uns 05JUN13 2.2
001 visit3 06JUN13 3.0
001 uns 07JUN13 3.1
001 uns 08JUN13 3.2
001 uns 01JUN13 3.3
001 visit4 02JUN13 4.0
001 visit5 03JUN13 5.0
001 uns 04JUN13 5.1
001 uns 05JUN13 5.2
001 visit6 06JUN13 6.0
001 uns 07JUN13 6.1
001 uns 08JUN13 6.2
Now prepare your Base SAS and
Advance SAS Certification Exams with Online Mock tests: http://exam.sankhyana.com
data test;
input Cust_ID $ visit $ visdt date7.;
format visdt date7.;
cards;
001 uns 01jun13
001 visit1 02jun13
001 visit2 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit3 06jun13
001 uns 07jun13
001 uns 08jun13
001 uns 01jun13
001 visit4 02jun13
001 visit5 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit6 06jun13
001 uns 07jun13
001 uns 08jun13
;
run;
We want the output like:
Cust_ID Vist visdt Visitseq
001 uns 01JUN13 0.1
001 visit1 02JUN13 1.0
001 visit2 03JUN13 2.0
001 uns 04JUN13 2.1
001 uns 05JUN13 2.2
001 visit3 06JUN13 3.0
001 uns 07JUN13 3.1
001 uns 08JUN13 3.2
001 uns 01JUN13 3.3
001 visit4 02JUN13 4.0
001 visit5 03JUN13 5.0
001 uns 04JUN13 5.1
001 uns 05JUN13 5.2
001 visit6 06JUN13 6.0
001 uns 07JUN13 6.1
001 uns 08JUN13 6.2
This comment has been removed by the author.
ReplyDeletedata test2;
ReplyDeleteset test;
freq=0;
do freq=1 to 16;
if visit ="uns" then do;
freq=freq+0.1;
end;
else if visit in ("visit1","visit2","visit2","visit3","visit4","visit5","visit6") then do;
freq=(freq+1);
freq=floor(freq);
end;
output;
end;
run;
i tried the above.. its not working
atlast i have found the closest answer:
ReplyDeletedata test2;
set test;
retain visiteq 0;
if visit ne "uns" then do;
visiteq=floor(visiteq+1);
end;
visiteq=visiteq+0.1;
run;
data test2;
ReplyDeleteset test;
if substr(visit,1,1)='v' then visitseq=floor(visitseq+1);
else if substr(visit,1,1)='u' then visitseq+0.1;
run;
data test1;
ReplyDeleteset test;
if visit='uns' then visitseq+0.1;
else if indexc(visit,'v0123456789') > 0 then visitseq=input(compress(visit,'0123456789','K'),??best.);
run;
data test1;
ReplyDeleteset test;
if visit='uns' then visitseq+0.1;
else if indexc(visit,'v0123456789') > 0 then visitseq=input(compress(visit,'0123456789','K'),??best.);
run;
data ra1(drop=freq freq_);
ReplyDeleteset test;
if visit='uns' then freq+0.1;
else freq= .;
if visit ne 'uns' then freq_+1;
visitseq=put(sum(freq_,freq),8.1);
run;
data test;
ReplyDeleteinput Cust_ID $ visit $ visdt date7.;
retain visitseq 0;
drop c;
c=substr(visit,length(visit),1);
if(upcase(c)=lowcase(c))then visitseq=c;
else visitseq=visitseq+.1;
format visdt date7.;
cards;
001 uns 01jun13
001 visit1 02jun13
001 visit2 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit3 06jun13
001 uns 07jun13
001 uns 08jun13
001 uns 01jun13
001 visit4 02jun13
001 visit5 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit6 06jun13
001 uns 07jun13
001 uns 08jun13
;
run;
proc print;
run;
Please try the below code,
ReplyDeletedata test;
input Cust_ID $ visit $ visdt date7.;
visitn=input(compress(visit,,'kd'),2.);
order=_n_;
format visdt date7.;
cards;
001 uns 01jun13
001 visit1 02jun13
001 visit2 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit3 06jun13
001 uns 07jun13
001 uns 08jun13
001 uns 01jun13
001 visit4 02jun13
001 visit5 03jun13
001 uns 04jun13
001 uns 05jun13
001 visit6 06jun13
001 uns 07jun13
001 uns 08jun13
;
run;
proc sort data=test;
by order;
run;
data test_;
set test;
by order;
retain seq 0.1;
if visit ne "uns" then do;
if first.order then seq=visitn;
end;
run;
proc sort data=test_;
by seq;
run;
data new;
set test_;
by seq;
retain visitseq;
if first.seq then visitseq=seq;
else visitseq+0.1;
run;
Thanks,
Jagadish
data test2;
ReplyDeleteretain t 0;
retain v 0;
set test;
if visit="uns" then
do;
t=t+0.1;
visiteq=v+t;
end;
else
do;
v=v+1;
visiteq=v;
end;
drop t v;
run;