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

Hi,

Here is the code I have following code for N1 variable in the sample dataset below:


proc sort data=dat.TestData out= test1;
by trt;
data test2;
set test1;
N1 + 1;
by trt;
if first.trt then N1 = 1;
run;

 

I need to add N2 variable. It's a sequential number with same value for first 12 records within a TRT group:

IDTRTN1N2
101_017200024TRT-A11
101_330700016TRT-A21
102_0235003133TRT-B12
102_0235009018TRT-B22
102_0240009014TRT-B32
102_0240009014TRT-B42
102_0301006034TRT-B52
102_0301006034TRT-B62
102_0800009074TRT-B72
102_0805003099TRT-B82
102_0808009078TRT-B92
102_1302003096TRT-B102
103_010500013TRT-B112
103_011100001TRT-B122
103_011400013TRT-B133
103_011500002TRT-B143
103_011500002TRT-B153
103_011500015TRT-B163
103_011700008TRT-B173
103_011900013TRT-B183
103_011900020TRT-B193
103_012200004TRT-C14
103_012200005TRT-C24
103_012400003TRT-C34
103_012700022TRT-C44
103_012700022TRT-C54

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

You need a way to make sure to change N2 when it becomes "stale", i.e. when the same value has been established for 12 records:

 

data test2;
  set test1;
  by trt;
  N1 + 1;
  if first.trt then N1 = 1;
  if first.trt or lag11(n2)=n2 then n2+1;
run;

 

Editted note:  If a treatment has, say 30 records, then I presume the first 12 records have one value for N2, then the next 12 have the next N2 value, and a 3rd N2 value is used for the last 6 records. 

--------------------------
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

--------------------------

View solution in original post

6 REPLIES 6
novinosrin
Tourmaline | Level 20

add this to code



if first.trt then N2 + 1;

A cleaner way would be

 

if first.trt then do;
 N1=1;
N2+1;
end;

 

Shain22
Calcite | Level 5

Unfortunately, that's not what I need. If you see the sample data, the N2 repeats with in the group for 1st 12 records.

mkeintz
PROC Star

You need a way to make sure to change N2 when it becomes "stale", i.e. when the same value has been established for 12 records:

 

data test2;
  set test1;
  by trt;
  N1 + 1;
  if first.trt then N1 = 1;
  if first.trt or lag11(n2)=n2 then n2+1;
run;

 

Editted note:  If a treatment has, say 30 records, then I presume the first 12 records have one value for N2, then the next 12 have the next N2 value, and a 3rd N2 value is used for the last 6 records. 

--------------------------
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

--------------------------
Shain22
Calcite | Level 5

Thank you!

Tom
Super User Tom
Super User

Just increment N2 when you start a new block of 12.  Either because you started an new TRT group or you reach n1=13,25,etc.

data want;
  set have;
  by trt;
  n1+1;
  if first.trt then n1=1;
  n2+(1=mod(n1,12));
run;
Shain22
Calcite | Level 5

Thanks...very creative solution!

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 6 replies
  • 780 views
  • 1 like
  • 4 in conversation