## calculate the frequency of positive and negative values by id

Hello,

I have this table

id variation

1     0.258

1      -0.53

2       0.563

2       -0.386

2     -0.568

I want to calculate the number of negative and positive variations by id. the table that I want

id variation count

1   positive   1

1   negatif     1

2   positive   1

2   negatif     2

8 REPLIES 8

## Re: calculate the frequency of positive and negative values by id

Use a format.

``````proc format;
value pos_neg_fmt
low - 0 = 'Negative'
0 - high = 'Positive';
run;

proc freq data=have;
table ID*Variation / list;
format variation pos_neg_fmt.;
run;``````

@sasphd wrote:

Hello,

I have this table

id variation

1     0.258

1      -0.53

2       0.563

2       -0.386

2     -0.568

I want to calculate the number of negative and positive variations by id. the table that I want

id variation count

1   positive   1

1   negatif     1

2   positive   1

2   negatif     2

## Re: calculate the frequency of positive and negative values by id

Thank you very much Reeza.

How I can do a z-test to test if the proportion of positive variation is equal between id 1 and 2?

## Re: calculate the frequency of positive and negative values by id

PROC TTEST or PROC ANOVA

## Re: calculate the frequency of positive and negative values by id

please can you give the program pour z-test with Anova or ttest  Ksharp
Super User

## Re: calculate the frequency of positive and negative values by id

I think should use Relative Risk . since it is really a contingency table.

@Rick_SAS  wrote a blog about it before.

https://blogs.sas.com/content/iml/2017/07/05/test-equality-two-proportions-sas.html

https://blogs.sas.com/content/iml/2015/10/26/exact-tests-proc-freq.html

https://blogs.sas.com/content/iml/2015/08/21/she-ranger.html

```data have;
input id variation ;
cards;
1     0.258
1      -0.53
2       0.563
2       -0.386
2     -0.568
;

data temp;
set have;
sign=sign(variation);
run;
proc freq data=temp noprint;
table id*sign/list out=want;
run;

proc freq data=want;
table id*sign/relrisk riskdiff chisq;
weight count;
run;```

## Re: calculate the frequency of positive and negative values by id

Just for completeness, where should exactly zero values fit?

## Re: calculate the frequency of positive and negative values by id

in positive!  Ksharp
Super User

## Re: calculate the frequency of positive and negative values by id

``````data have;
input id variation ;
cards;
1     0.258
1      -0.53
2       0.563
2       -0.386
2     -0.568
;

data temp;
set have;
sign=sign(variation);
run;
proc freq data=temp noprint;
table id*sign/list out=want;
run;``````
Discussion stats
• 8 replies
• 393 views
• 6 likes
• 4 in conversation