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

Hello all,

    I want to look at the rows in my table and assign a counting variable to increase by 1 if a certain set of conditions are met. 

In the following table, for each study ID WANT needs to start counting at the LEGACY value. PLT lists the initial donation number for each participant. Then it needs to increase by 1 if the DonationType=1. If the donation type is ^=1 then the cell needs to be missing. Right now, I can get it to fill in the correct value for the first and instance of each study ID, but it will not fill down. 

STUDYIDDonationNumbDONTYPELEGACYPLTWANTHAVE
12119101010
1221. 1111
1232. . 
1241. 12 
1251  13 
1261. 14 
131200.0
1321. 11
1331. 2 

 

Data want;
	set have;

retain HAVE;
HAVE=lag(HAVE);
if DONATIONNUMB=1 then HAVE=PLT;
if (DONATIONNUMB>1 AND DONATIONTYPE=1) then HAVE=(HAVE)+1;
else if(DONATIONNUMB>1 AND DONATIONTYPE>1) then HAVE=HAVE+0;
run;
 
Any help is greatly appreciated!
 
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
data want;
set have;
by studyid;
if first.studyid
then do;
  _have = plt;
  if dontype = 1
  then have = _have;
  else have = .;
end;
else do;
  if dontype = 1
  then do;
    _have + 1; /* sum statement causes automatic retain */
    have = _have;
  end;
  else have = .;
end;
drop _have;
run;

The LAG() can't be reasonably used with a newly created variable; it fills its queue every time it is called, and at that moment the variable is still missing, or has the previous value anyway, because of the RETAIN.

View solution in original post

1 REPLY 1
Kurt_Bremser
Super User
data want;
set have;
by studyid;
if first.studyid
then do;
  _have = plt;
  if dontype = 1
  then have = _have;
  else have = .;
end;
else do;
  if dontype = 1
  then do;
    _have + 1; /* sum statement causes automatic retain */
    have = _have;
  end;
  else have = .;
end;
drop _have;
run;

The LAG() can't be reasonably used with a newly created variable; it fills its queue every time it is called, and at that moment the variable is still missing, or has the previous value anyway, because of the RETAIN.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 422 views
  • 0 likes
  • 2 in conversation