Hello,
I basically want to find a string of characters from an observation. For example, some of the observations in a diagnosis column are:
Z34.3, Z78.0, Z59.1, Z63.2 |
Z53.3, Z54.3, Z59.2, Z87.3 |
I want to find how many Z59 are there in this column. How do I write the code in SAS so that I get the frequency of Z59 irrespective of what's there after the point?
Something like this?
data have;
input diagnosis $ 1-30;
cards;
Z34.3, Z78.0, Z59.1, Z63.2
Z53.3, Z54.3, Z59.2, Z87.3
;
run;
data want;
set have;
countZ59=0;
do i=1 to countc(diagnosis,',');
if scan(diagnosis,i,',')=:' Z59'
then countZ59=countZ59+1;
end;
drop i;
run;
proc means data=want SUM nway;
var countZ59;
run;
/* end of program */
Koen
data want;
set have;
count=0;
do i=1 to countw(diagnosis,',');
if scan(diagnosis,i,',')=:'Z59' then count=count+1;
end;
drop i;
run;
Hello,
Is this what you are after?
data have;
infile cards delimiter=',';
input zvar $ @@;
z2=scan(zvar,1,'.');
cards;
Z34.3, Z78.0, Z59.1, Z63.2
Z53.3, Z54.3, Z59.2, Z87.3
;
run;
PROC FREQ data=have;
tables z2 / out=want;
run;
proc print data=want;
run;
/* end of program */
Koen
Hey,
Thank you for your reply. No, I actually only want to know how many Z59's are there in the column. So in this example, there are 2 but I have 800 observations. Is there another way to do it? Thanks!
Something like this?
data have;
input diagnosis $ 1-30;
cards;
Z34.3, Z78.0, Z59.1, Z63.2
Z53.3, Z54.3, Z59.2, Z87.3
;
run;
data want;
set have;
countZ59=0;
do i=1 to countc(diagnosis,',');
if scan(diagnosis,i,',')=:' Z59'
then countZ59=countZ59+1;
end;
drop i;
run;
proc means data=want SUM nway;
var countZ59;
run;
/* end of program */
Koen
Hello,
I have just noticed my solution does not work with 'Z59' in the very first 3 positions.
Because my search is for ' Z59' (remark the leading space).
Cheers,
Koen
Like this?
proc sql;
select scan(ZVAR,1) as Z2, count(*) as COUNT from HAVE group by 1;
data have; input x $80.; cards; Z34.3, Z78.0, Z59.1, Z63.2 Z53.3, Z54.3, Z59.2, Z87.3 Z59.2, Z87.3, Z59.2, Z87.3 ; run; proc sql; select sum(count(x,'Z59')) as n from have; quit;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.