i have a data set where 20 study participants went through 24 tests (8 conditions each repeated three times). my data base consists of the subject number (idsub) and test condition (either "cond" or "fact"). i like to add another column to indicate repeated conditions (REPT). I appreciated any suggestion. the data below is from one subject (idsub=1) and REPT is the variable that i would like to add.
idsub cond fact REPT
1 1 NOEO 1
1 3 YOBL 1
1 2 NOEC 1
1 4 YOSC 1
1 3 YOBL 2
1 2 NOEC 2
1 1 NOEO 2
1 4 YOSC 2
1 3 YOBL 3
1 2 NOEC 3
1 4 YOSC 3
1 1 NOEO 3
If I understand you correctly, then here is how I would do it. I added data for idsub=2 and handled multiple values of the idsub variable as well. Let me know if it works for you.
data have;
input idsub cond fact $;
datalines;
1 1 NOEO
1 3 YOBL
1 2 NOEC
1 4 YOSC
1 3 YOBL
1 2 NOEC
1 1 NOEO
1 4 YOSC
1 3 YOBL
1 2 NOEC
1 4 YOSC
1 1 NOEO
2 1 NOEO
2 3 YOBL
2 2 NOEC
2 4 YOSC
2 3 YOBL
2 2 NOEC
2 1 NOEO
2 4 YOSC
2 3 YOBL
2 2 NOEC
2 4 YOSC
2 1 NOEO
;
data want(drop=rc);
if _n_ = 1 then do;
dcl hash h ();
h.defineKey ('cond', 'fact');
h.defineData ('REPT');
h.defineDone ();
end;
set have;
by idsub;
if first.idsub then do;
rc=h.clear();
REPT=0;
end;
if h.find() ne 0 then REPT=1;
else REPT+1;
h.replace();
run;
If I understand you correctly, then here is how I would do it. I added data for idsub=2 and handled multiple values of the idsub variable as well. Let me know if it works for you.
data have;
input idsub cond fact $;
datalines;
1 1 NOEO
1 3 YOBL
1 2 NOEC
1 4 YOSC
1 3 YOBL
1 2 NOEC
1 1 NOEO
1 4 YOSC
1 3 YOBL
1 2 NOEC
1 4 YOSC
1 1 NOEO
2 1 NOEO
2 3 YOBL
2 2 NOEC
2 4 YOSC
2 3 YOBL
2 2 NOEC
2 1 NOEO
2 4 YOSC
2 3 YOBL
2 2 NOEC
2 4 YOSC
2 1 NOEO
;
data want(drop=rc);
if _n_ = 1 then do;
dcl hash h ();
h.defineKey ('cond', 'fact');
h.defineData ('REPT');
h.defineDone ();
end;
set have;
by idsub;
if first.idsub then do;
rc=h.clear();
REPT=0;
end;
if h.find() ne 0 then REPT=1;
else REPT+1;
h.replace();
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.