Solved
Contributor
Posts: 69

# Create an enumeration variable with a condition

Hi, I want to create a variable to count the number of values greater than 0 and another one for values less than 0. This is my code, but it is not working for the first register of the category.

data work.cb_dc6;

set work.cb_dc5;

by block;

if (dif>=0) then posi+1;

if (dif<0) then nega+1;

if first.block then posi=0 and nega=0;

run;

Look register #1 CRETEXT1, column posi should begin with 1 no 0. Same problem with register 10 ALICORC1, register 10 for nega column should be 1:

Accepted Solutions
Solution
‎01-22-2015 03:43 PM
Super User
Posts: 23,928

## Re: Create an enumeration variable with a condition

Set your variables to 0 before you increment them otherwise you're setting them after but not incrementing the variables you need to keep track of.

data work.cb_dc6;

set work.cb_dc5;

by block;

retain posi nega;

if first.block then do;

posi=0; nega=0;

end;

if (dif>=0) then posi+1;

if (dif<0) then nega+1;

run;

All Replies
Posts: 3,864

## Re: Create an enumeration variable with a condition

Maybe you should initialize before you increment. :smileylaugh: AND you need do; posi=0; nega=0; end; not and.

Contributor
Posts: 69

## Re: Create an enumeration variable with a condition

I'm not sure about what you try to say, this is the change, same problem =(

data work.cb_dc6;

set work.cb_dc5;

by block;

retain posi nega;

if (dif>=0) then posi+1;

if first.block then posi=0;

if (dif<0) then nega+1;

if first.block then nega=0;

run;

Solution
‎01-22-2015 03:43 PM
Super User
Posts: 23,928

## Re: Create an enumeration variable with a condition

Set your variables to 0 before you increment them otherwise you're setting them after but not incrementing the variables you need to keep track of.

data work.cb_dc6;

set work.cb_dc5;

by block;

retain posi nega;

if first.block then do;

posi=0; nega=0;

end;

if (dif>=0) then posi+1;

if (dif<0) then nega+1;

run;

Contributor
Posts: 69

## Re: Create an enumeration variable with a condition

Oh! now I understand it! Thank your very much!!

🔒 This topic is solved and locked.