BookmarkSubscribeRSS Feed
CathyVI
Pyrite | Level 9

Hello,

Hope someone can help me out. I have a dataset where someone can complete 2 or 3 forms (e.g., form A, form B & form C).
I want a count of someone in Form A only, Form B only and Form C only and those who filled more than 1 form (mult_form). If they complete form A and form B, they should be counted as mult_form. For example, ID 102 & 104 to be counted as multi_form. Here is my dummy data.

data dummy;
input id$ 1-3 FormA FormB FormC ;
infile datalines ;
datalines;
101 1 0 .
101 . 0 .
102 1 0 .
102 . 1 .
103 . . 0
103 . . 1
104 1 0 .
104 . . 1
105 . 1 .
;
;
run;

Thanks in advance.

5 REPLIES 5
ballardw
Super User

I am confused that you even mention 25 or 45 as don't see anything actually related to those. Please use VARIABLE names and shown values to discuss rules. Otherwise we have to interpret (translate: make a lot of guesses) what we think you mean. For example, I do not understand what "Form A only" means. You show variables with some of that text in the name but do not really tell use how to identify a "Form A Only" record, id, group or what have.

 

And please provide an example of two of pointing which variables, values on which rows are used to satisfy some of these criteria.

 

I can't tell what you are starting with. Or really what the result should look like. Especially since none of the values of FormType are "Form C" but you show results in "FormC30_plus".

Patrick
Opal | Level 21

The following code will set variable MultiForm to 1 if for an ID more than one Form variable got a value of 1.

data have;
  input id$ 1-3 FormA FormB FormC;
  infile datalines;
  datalines;
101 1 . . 
101 1 0 . 
102 1 0 . 
102 . 1 . 
103 . . 0 
103 . . 1 
104 1 0 . 
104 . . 1
105 . 1 . 
;
run;

proc sql;
/*  create table want as*/
  select i.*,sum(FormA,FormB,FormC)>1 as MultiForm
  from
  (
    select 
      id,
      max(FormA) as FormA,
      max(FormB) as FormB,
      max(FormC) as FormC
    from have
    group by id
  ) i
  ;
quit;

Patrick_0-1711002653982.png

 

 

CathyVI
Pyrite | Level 9

@Patrick @ballardw based on your response, if I want to have a count of formA only, formB only, formC only and multi_form how will i get the frequency count? 

I guess what am saying is how would I count formAonly without counting (where 102=1) as part of formA. 

Please can I learn this using data step? Thanks

Patrick
Opal | Level 21

@CathyVI Below one way when using SQL

proc sql;
/*  create table want as*/
  select 
    *,
    ifn(MultiForm=1 and FormA=1,0,FormA) as FormA_only,
    ifn(MultiForm=1 and FormB=1,0,FormB) as FormB_only,
    ifn(MultiForm=1 and FormC=1,0,FormC) as FormC_only
  from
  (
    select 
      *,
      sum(FormA,FormB,FormC)>1 as MultiForm
    from
    (
      select 
        id,
        max(FormA) as FormA,
        max(FormB) as FormB,
        max(FormC) as FormC
      from have
      group by id
    ) 
  )
  ;
quit;
Mazi
Pyrite | Level 9
data dummy;
input id$ 1-3 FormA FormB FormC ;
infile datalines ;
datalines;
101 1 0 . 
101 . 0 . 
102 1 0 . 
102 . 1 . 
103 . . 0 
103 . . 1 
104 1 0 . 
104 . . 1
105 . 1 . 
;
;
run;

data want;
	do until(last.id);
		set dummy;
		by id;
		if formA then _forma=1;
		if formB then _formb=1;
		if formC then _formc=1;
	end;
	if sum(of _form:)>1 then multiForm=1;
	else multiForm=0;
	if multiForm=0 then do;
		if _forma then formAOnly=1;
		else if _formB then formBOnly=1;
		else if _formC then formCOnly=1;
	end;
	output;
	drop _:;
run;

Can you try this?

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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