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

I have a variable that looks kind of like this:

 

variable1
FLZZZZZ
DFZZZZZ
DGLZZZZ
FMNZZZZ
DZZZZZZ
FGZZZZZ
FGLZZZZ

I need to count the occurrences of F 

the occurrences of L

the occurrences of L and F

and the occurrences of L or F

 

so that it looks kind of like this:

 

Just_F  |  Just_L  | L_and_F | L_or_F

    3             1              2              4

 

I've been using proc sql and trying COUNT

count(variable1,'FL')
sum(case when variable1 = 'F' then 1 else 0 end) as Just_F,
sum(case when variable1 = 'L' then 1 else 0 end) as Just_L,
sum(case when variable1 = 'F' or variable1 = 'L' then 1 else 0 end) as L_or_F,
sum(case when variable1 = 'F' and variable1 = 'L' then 1 else 0 end) as L_and_F

what I get from proc sql sum cases is all 0 I think because it's not taking into account the other characters.

 

Please Help! 🙂

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You can do this in SQL, but a data step seems much more straightforward to me, since its a single pass of the data as well.

 

proc sql ;
	create table want as select sum(FCount>0) as FTotal, sum(LCount>0) as LTotal, 
		sum(fcount>0 and lcount>0) as FLTotal, sum(fcount>0 or lcount>0) as 
		FLoptional from (select a.*, countc(var1, 'F') as FCount, countc(var1, 'L') 
		as LCount from have as a);
quit;

View solution in original post

4 REPLIES 4
Reeza
Super User

You're checking if the variable is equal to the F/L rather than counting them in that code. 

 

Use COUNT or COUNTW. 

Shmuel
Garnet | Level 18

I'm not sure you can do it with proc sql.

 

Using datastep you can try:

data have;
   input var1 $;
cards;
FLZZZZZ
DFZZZZZ
DGLZZZZ
FMNZZZZ
DZZZZZZ
FGZZZZZ
FGLZZZZ
; run;

data _null_;
 set have end=eof;
   retain Just_f Just_L L_and_F L_or_F ;
   array count  Just_f Just_L L_and_F L_or_F ;
   count_F=0; count_L=0;

   do i=1 to length(var1);
        char = substr(var1, i, 1);
        if char = 'F' then do; count_F=1; Just_F +1; end; else
        if char = 'L' then do; count_L=1; Just_L +1; end;
  end;
  if count_F and count_l then L_and_F +1;
  if Count_F or count_L then L_or_F +1;

  if eof then put Just_f= Just_L= L_and_F= L_or_F= ;
run;
Shmuel
Garnet | Level 18

You may prefer use function COUNTC (thanks to @Reeza😞

data have;
   input var1 $;
cards;
FLZZZZZ
DFZZZZZ
DGLZZZZ
FMNZZZZ
DZZZZZZ
FGZZZZZ
FGLZZZZ
; run;

data _null_;
 set have end=eof;
   retain Just_f Just_L L_and_F L_or_F ;
   array count  Just_f Just_L L_and_F L_or_F ;

   count_F = countc(var1,'F');
   count_L = countc(var1,'L');
   Just_F + count_F;
   Just_L + count_L;
   L_or_F = L_or_F + countc(var1,'LF');
   if count_F and count_L then L_and_F +1;

  if eof then put Just_f= Just_L= L_and_F= L_or_F= ;
run;
Reeza
Super User

You can do this in SQL, but a data step seems much more straightforward to me, since its a single pass of the data as well.

 

proc sql ;
	create table want as select sum(FCount>0) as FTotal, sum(LCount>0) as LTotal, 
		sum(fcount>0 and lcount>0) as FLTotal, sum(fcount>0 or lcount>0) as 
		FLoptional from (select a.*, countc(var1, 'F') as FCount, countc(var1, 'L') 
		as LCount from have as a);
quit;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 5656 views
  • 2 likes
  • 3 in conversation