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

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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