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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
Reeza
Super User
Is the rule 4 1's, 4 2's, 4 #s etc? or something else?

If it's every 4 something like this:

data want;
set have;
retain count 0;

if mod(_n_, 4) = 1 then count+1;

run;
PeterClemmensen
Tourmaline | Level 20

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;

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
  • 2 replies
  • 552 views
  • 3 likes
  • 3 in conversation