Help using Base SAS procedures

resetting or creating a new counter variable

Reply
New Contributor wcw
New Contributor
Posts: 3

resetting or creating a new counter variable

Hi, I have a dose number counter variable  (ORIGINAL DOSENUM) that, for some, does not start at "1" (ID 1). I want to reset this (or create a new counter variable) where all DOSENUMs start at "1" for each ID (as is the case for ID 2), i.e., DESIRED DOSENUM. Some original DOSENUMs start at 3, or 4 or 5, etc. I was able to create a new counter using , in the data step: "by ID ORIGINAL DOSENUM" followed by "if first.original dosenum then count+1", but this does not start the counter for each ID. Hope this is clear. Thanks.

data TEST1; input USUBJID $ EXSPID; cards; 01     3 01     3 01     3 01     3 01     4 01     4 01     4 01     4 01     5 01     5 01     5 01     5 02     1 02     1 02     1 02     1 02     2 02     2 02     2 02     2 02     3 02     3 02     3 02     3 ; run; Proc Sort data=test1; by USUBJID EXSPID; run; data test2; set test1; by USUBJID EXSPID; if first.exspid then count+1; Proc Print; run;


Capture.JPG
Super User
Super User
Posts: 7,407

Re: resetting or creating a new counter variable

Hi,

Well, formatting your code so I can read it would help :smileyshocked:

You may be missing a retain:

data want;

     set have;  /* Assuming sorted! */

     by id;

     retain new_id;

     if first.id then new_id=1;

     else new_id=new_id+1;

run;

New Contributor wcw
New Contributor
Posts: 3

Re: resetting or creating a new counter variable

hey thanks (sorry 1st time user here):smileyblush:

yes, I was thinking I needed a retain somewhere

PROC Star
Posts: 7,363

Re: resetting or creating a new counter variable

Your problem isn't that you're missing a retain statement, as using the sum function like you did automatically retains the variable. I think you need an extra if statement like:

data test2;

  set test1;

  by USUBJID EXSPID;

  if first.USUBJID then count=1;

  else if first.exspid then count+1;

run;

New Contributor wcw
New Contributor
Posts: 3

Re: resetting or creating a new counter variable

aha perfect, thank you!Smiley Happy

Ask a Question
Discussion stats
  • 4 replies
  • 6778 views
  • 1 like
  • 3 in conversation