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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.