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