BookmarkSubscribeRSS Feed
JakeAZ
Calcite | Level 5

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 varA1varA2varA3varB1varB2varB3
Office A YesNo.NoNoYes
Office B.YesSometimes YesNoNo
Office CNoSometimes YesYes.Yes
Office D.YesYesYesNoNo

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 varATotalvarBTotal
Office AYesNo
Office A NoNo
Office A .Yes
Office B.Yes
Office BYesNo
Office BSometimes No
Office CNoYes
Office CSometimes .
Office CYesYes
Office D.Yes
Office DYesNo
Office DYesNo

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 

10 REPLIES 10
art297
Opal | Level 21

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;

JakeAZ
Calcite | Level 5

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;

art297
Opal | Level 21

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;

JakeAZ
Calcite | Level 5

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;

art297
Opal | Level 21

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?

JakeAZ
Calcite | Level 5

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.

Patrick
Opal | Level 21

Art

I believe the OT just struggles with the syntax of the dim() function as part of the do-loop.

Patrick
Opal | Level 21

Assuming all your arrays have the same number of elements then:

......

Do I=1 to dim(a);

   PhysicalT=a(I);

   EducationT=b(I);

......

art297
Opal | Level 21

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;

Patrick
Opal | Level 21

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.

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore 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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 10 replies
  • 4805 views
  • 0 likes
  • 3 in conversation