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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

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
  • 1 reply
  • 307 views
  • 0 likes
  • 2 in conversation