BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Gayatrikunchay
Obsidian | Level 7

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?

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

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

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26
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;

 

--
Paige Miller
sbxkoenk
SAS Super FREQ

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

Gayatrikunchay
Obsidian | Level 7

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!

sbxkoenk
SAS Super FREQ

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

sbxkoenk
SAS Super FREQ

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

ChrisNZ
Tourmaline | Level 20

Like this?

proc sql;
   select scan(ZVAR,1) as Z2, count(*) as COUNT from HAVE group by 1;

 

Ksharp
Super User
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;
sbxkoenk
SAS Super FREQ

@Gayatrikunchay : a wealth of solutions to choose from, I would say.

Cheers,

Koen

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1927 views
  • 1 like
  • 5 in conversation