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

Let's say our total observations is 6000, if 500 participants answered "5" as the response, the expected frequency output for "sum" should be for example this:

 

              frequency

    1           1000 

    2            2000

    3           1000

    4            1500

 

thank you!

Shmuel
Garnet | Level 18

@mandan414 wrote:

Let's say our total observations is 6000, if 500 participants answered "5" as the response, the expected frequency output for "sum" should be for example this:

 

              frequency

    1           1000 

    2            2000

    3           1000

    4            1500

 

thank you!


 

Within the small sample:

data have;

   input AA_005 AA_010 AA_015 AA_020 AA_025 AA_030 AA_035;
   datalines;
1 1 2 3 5 2 1
2 2 5 1 3 3 2
5 4 1 1 2 3 1 
;
run;

do you expect to sum=1, as only one observation does not contain a score of 5 ?

Shmuel
Garnet | Level 18

@mandan414 wrote:

Hi Samuel,

 

I don't want to sum up the value for AA_% variables. I want to sum up the number of observations. For example, I have  6000 observations responded to AA_% variables as 1=excellent, .......... 5=bad. I would like to calculate the total observations answered 1 or 2 or 3 or 4 to each of AA_% variable. Thanks.


Option-1. Next is the result of one of my previous post, including the code:

image.png

you can sum totals (last row) of any score(s) you want.

 

Options-2. Use @andreas_lds code to transpose and run proc freq or compute any score value using RETAIN within a data step.

 

Option-3 is to make to adapt my code, to the requirement:

data want;
 set have end=done;
     retain sum 0;
     array si {7} AA_:;
     do i=1 to dim(si);
        if sm(si(i)) ^= 5 then sum+1; /* equal to sum=sum+1 */
     end;
     keep sum;
	 label sum='Total number of scoes < 5 ';
run;
proc print data=want label; run;

There are more options but I prefer not to confuse you with it.

mandan414
Fluorite | Level 6

Thank you so much. How about my array codes? I think that works too, am I right?

Shmuel
Garnet | Level 18

@mandan414 wrote:

Thank you so much. How about my array codes? I think that works too, am I right?


You probably point to your code:

data new;
    set mydata;
    array myarray AA_:;
    sum = 0;
    do i=1 to dim(myarray);
        if myarray[i] not in 5 then do;
           sum = sum + 1
        end;
    end;
run;
 

No, this code will not result as you want. 

For scores row like: 

5 4 1 1 2 3 1

your code will result in 6 instead of 1.

 

To correct your code use (check it) next one:

data new;
    set mydata end=done;
    retain sum;   /* line added */
    array myarray AA_:;
    sum = 0;
    to_add = 0;  /* new temporary variable */
    do i=1 to dim(myarray);
        if myarray[i] not in 5 then to_add = 1; /* flag, some score is < 5 */
     end;
     sum = sum + to_add; /* count obserbations with any score < 5 */
     if done then output;   /* save on end of input */
     keep sum;
run;

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
  • 20 replies
  • 1790 views
  • 0 likes
  • 4 in conversation