BookmarkSubscribeRSS Feed
MigMaster12
Calcite | Level 5

Greetings,

 

I have a question about the following example dataset of 3 subjects and condition level responses (0, 1, 2) for each visit:

 

Subjectvisitcondition_level
A10
A20
A31
B10
B21
B30
B40
C11
C22
C31

 

What I want to do is specify for all the subjects that they cannot regress on the condition level: i.e. once a person reach 1 or 2, they cannot go back to 0 or 1, and must instead keep the previous level. For the dataset I would like to modify it to the following:

 

Subjectvisitcondition_level
A10
A20
A31
B10
B21
B31
B41
C11
C22
C32

 

I've tried using the lag() function and retain statements, but so far nothing has worked for me. Perhaps an iterative DO loop will be needed?

 

I'd appreciate any help/advice here. 

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Hi @MigMaster12  Assuming I understand your requirement -

 



data have;
input Subject $	visit	condition_level;
cards;
A	1	0
A	2	0
A	3	1
B	1	0
B	2	1
B	3	0
B	4	0
C	1	1
C	2	2
C	3	1
;

data Want;
 do until(last.subject);
  set have;
  by subject;
  if _n_ then _condition_level=condition_level;
  if not first.subject  and condition_level in (1,2) then _n_=0;
  output;
 end;
 drop condition_level;
 rename _condition_level=condition_level;
run;

The only thing I'm not quite clear is C reached condition level in 1st visit. Therefore to meet your requirement, I have assumed to start the check from the 2nd record of each subject

mkeintz
PROC Star

I suggest you keep a _HISTORIC_MAX variable, and compare it to each incoming condition_level.  Update each as appropriate:

 

data have;
input Subject $	visit	condition_level;
cards;
A	1	0
A	2	0
A	3	1
B	1	0
B	2	1
B	3	0
B	4	0
C	1	1
C	2	2
C	3	1
;
data want (drop=_:);
  set have;
  by subject;
  retain _historic_max;
  if first.subject then _historic_max=condition_level;  else
  if condition_level>_historic_max then _historic_max=condition_level; else
  condition_level=max(_historic_max,condition_level);
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Ksharp
Super User
data have;
input Subject $	visit	condition_level;
cards;
A	1	0
A	2	0
A	3	1
B	1	0
B	2	1
B	3	0
B	4	0
C	1	1
C	2	2
C	3	1
;
data want;
 set have;
 by subject;
 retain want;
 if first.subject then call missing(want);
 want=max(want,condition_level)	;
run;;
Kurt_Bremser
Super User

Another, slightly different take:

data want;
set have;
by subject;
retain _cl;
if first.subject
then _cl = condition_level;
else do;
  condition_level = max(condition_level,_cl);
  _cl = condition_level;
end;
drop _cl;
run;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 948 views
  • 0 likes
  • 5 in conversation