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!
@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 ?
Yes.
@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:
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.
Thank you so much. How about my array codes? I think that works too, am I right?
@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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.