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

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:

any idea? Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

View solution in original post

4 REPLIES 4
data_null__
Jade | Level 19

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

fri0
Quartz | Level 8

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;

Reeza
Super User

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;

fri0
Quartz | Level 8

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 4 replies
  • 2640 views
  • 3 likes
  • 3 in conversation