Hi--
I am relatively new to SAS and I'm stuck. I have a dataset with sets of variables, which have to be combined. You can see in the table below -- I have a varA set and a varB set.
Office | varA1 | varA2 | varA3 | varB1 | varB2 | varB3 |
Office A | Yes | No | . | No | No | Yes |
Office B | . | Yes | Sometimes | Yes | No | No |
Office C | No | Sometimes | Yes | Yes | . | Yes |
Office D | . | Yes | Yes | Yes | No | No |
I need to combine all the varA’s into one variable and allthe varB’s into one variable. So, the data set should look like this:
Office | varATotal | varBTotal |
Office A | Yes | No |
Office A | No | No |
Office A | . | Yes |
Office B | . | Yes |
Office B | Yes | No |
Office B | Sometimes | No |
Office C | No | Yes |
Office C | Sometimes | . |
Office C | Yes | Yes |
Office D | . | Yes |
Office D | Yes | No |
Office D | Yes | No |
The other part to this is that there are many datasets that I have to run, so varA could go up to 9 in one dataset but only be 3 in another.
Any help is very appreciated. Thanks
data want (keep=office varaTotal varbTotal);
set have;
array a vara:;
array b varb:;
do i=1 to 3;
varaTotal=a(i);
varbTotal=b(i);
output;
end;
run;
Hi Art. Thank you for your help. I have a question though. I am trying to write this so the code will count the amount of variables for me. I the I should use DIM but I'm not sire how to use it.
data want (keep=Quest_ Program_Name PhysicalT PhysicalOngT
EducationT EducationOngT EmotionalT: EmotionalOngT SocialT SocialOngT);
set work.have
array a (*) Physical: ;
array b (*) Education:;
array c (*) Emotional:;
array d (*) Social:;
array e (*) PhysicalOng:;
array f (*) EducationOng:;
array g (*) EmotionalOng:;
array h (*) SocialOng:;
do i=1 to dim();
PhysicalT=a(i);
EducationT=b(i);
EmotionalT=c(i);
SocialT=d(i);
PhysicalOngT=e(i);
EducationOngT=f(i);
EmotionalOngT=g(i);
SocialOngT=f(i);
output;
end;
run;
data want (keep=Quest_ Program_Name PhysicalT PhysicalOngT
EducationT EducationOngT EmotionalT: EmotionalOngT SocialT SocialOngT);
set work.have
array a (*) Physical: ;
array b (*) Education:;
array c (*) Emotional:;
array d (*) Social:;
array e (*) PhysicalOng:;
array f (*) EducationOng:;
array g (*) EmotionalOng:;
array h (*) SocialOng:;
PhysicalT=dim(a);
EducationT=dim(b);
EmotionalT=dim(c);
SocialT=dim(d);
PhysicalOngT=dim(e);
EducationOngTdim(f);
EmotionalOngT=dim(g);
SocialOngT=dim(h);
run;
Hi Art. Thanks. It looks like it didn't work. When I ran your code each of the variables had a sum for all the responces and not the sperate responces.
I tried this and i also does not work.
data work.QAsFeq2 (keep=/*Quest_*/ Program_Name PhysicalT PhysicalOngT
EducationT EducationOngT EmotionalT: EmotionalOngT SocialT SocialOngT);
set work.QAsFeq;
array a (*) Physical: ;
array b (*) Education:;
array c (*) Emotional:;
array d (*) Social:;
array e (*) PhysicalOng:;
array f (*) EducationOng:;
array g (*) EmotionalOng:;
array h (*) SocialOng:;
do i=1 to dim (*);
PhysicalT=dim(a);
EducationT=dim(b);
EmotionalT=dim(c);
SocialT=dim(d);
PhysicalOngT=dim(e);
EducationOngT=dim(f);
EmotionalOngT=dim(g);
SocialOngT=dim(h);
output;
end;
run;
I'm not sure I understand what you are looking for. Are you looking for the number of non 'No' entries for all of the variables included in each array for each record?
Hi. No not the sum. I just want to combine all the responces (no, yes, ect.) into one variable for each of the variable sets.
this is what I want I trying to do:
varA1 varA2 varA 3 VaraTotal
offce1 yes yes no yes
office2 no yes no no
office2 yes yes no yes
yes
yes
no
no
no
Thank you for your help.
Art
I believe the OT just struggles with the syntax of the dim() function as part of the do-loop.
Assuming all your arrays have the same number of elements then:
......
Do I=1 to dim(a);
PhysicalT=a(I);
EducationT=b(I);
......
Patrick: Given the OP's original specs, I don't think that is what he is looking for. My guess (excuse the rushed coding) would be that he is looking for something like (including the original request):
data have;
informat Office $8.;
input Office & (varA1-varA3 varB1-varB3) ($);
cards;
Office A Yes No . No No Yes
Office B . Yes Sometimes Yes No No
Office C No Sometimes Yes Yes . Yes
Office D . Yes Yes Yes No No
;
data want1 (keep=office varaTotal varbTotal);
set have;
array a vara:;
array b varb:;
do i=1 to 3;
varaTotal=a(i);
varbTotal=b(i);
output;
end;
run;
data want2 (keep=office varaTotal varbTotal);
set have;
array a vara:;
array b varb:;
call missing(varaTotal);
call missing(varbTotal);
do over a;
varaTotal+(a in ('Sometimes','Yes'));
end;
do over b;
varbTotal+(b in ('Sometimes','Yes'));
end;
run;
Exactly. I believe the required result is your "data want1" except that the OT needs the do loop to be "do i=1 to dim(a);" so that the array a can have a varying number of elements.
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!
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.