BookmarkSubscribeRSS Feed
sasboy007
Calcite | Level 5

Hi all,

I'm appending data to an already created table.  I'm using proc sql below to bring back the highest ID value so that I can start my new counter where it left off.  My code is below but I'm running into some issues with my IF statment at the very bottom.  I don't think the counter is actually incrementing from the last database position.  Any insight is appreciated.

/*this brings back the highest numID*/
proc sql noprint;
   select max(numID)
      into :counter 
      from gate.randomTable;

quit;

data work.newData
mycounter=0;

(keep =
mycounter
x

);

retain
mycounter
x

;

set work.dataY;

if first.x then mycounter=&counter;

      mycounter + 1;

   run;

4 REPLIES 4
data_null__
Jade | Level 19

don't you need

BY X;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

You could also do it:

proc sql;

  create table TAB1 as

  select  MODEL,

          MONOTONIC() as NUMID

  from    SASHELP.CARS;

  create table TAB2 as

  select  MODEL,

          NUMID

  from    TAB1

  union all

  select  MODEL,

          MONOTONIC() + (select max(NUMID) from TAB1) as NUMID

  from    TAB1;

quit;

charley_sas
SAS Employee

I'm not sure where X comes from so I removed the references to it.  Try the following:

data work.newData

retain mycounter &counter;

set work.dataY;

mycounter + 1;

<other code goes here>;

run;

sasboy007
Calcite | Level 5

Hey thanks.  I completely forgot to add &counter to my RETAIN  statement.

I ended up using and it seems to work now.

mycounter=&counter;

mycounter+1;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 4 replies
  • 1699 views
  • 3 likes
  • 4 in conversation