BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Mscarboncopy
Pyrite | Level 9

Thank you, this works! I really appreciate it.

I have another question, if you do not mind, since I have your attention.

I need to add values from 1 to X to a numeric variable every time I run my code. The code is being use to clean data files weekly and each file has different data (records).

It is an identifying variable, let's call it ID=. 

Each record needs the unique number starting at 1 at first.

I wanted to be able to add the values at the starting point of the first record and be able to continue to assign that number from from where it left off. I would indicate in my code each week that starting point is 5 instead of 1, or 11 instead of 1 (as per example below).

 

File 1  has 4 records so the ID would be

ID 

1

2

3

4

 

When I run the code again it would pick up from where it left off

 

File 2

ID

5

6

7

8

9

10

File 3 would start at 11

11

etc

Is this possible?

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

To get started you need to store the last used number somewhere.

Then you can use that value as the initial value in your new data step.

 

For example you might want to use the maximum value from the previous dataset.  

proc sql noprint;
  select max(x) format=32. into :lastx trimmed 
  from FILE1
  ;
quit;

data file2;
  retain x &lastx;
  x+1;
  ... rest of data step ...
run;

Or you could make another single observation dataset to store the last value.

Example:

data lastx;
  x=0;
run;
proc print data=lastx; run;

data want lastx(keep=x);
  if _n_=1 then set lastx;
  if eof then output lastx ;
  set sashelp.class(obs=3) end=eof;
  x+1;
  output want;
run;

proc print data=want; run;
proc print data=lastx; run;

data want2 lastx(keep=x);
  if _n_=1 then set lastx;
  if eof then output lastx ;
  set sashelp.class(obs=4) end=eof;
  x+1;
  output want2;
run;

proc print data=want2; run;
proc print data=lastx; run;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

To get started you need to store the last used number somewhere.

Then you can use that value as the initial value in your new data step.

 

For example you might want to use the maximum value from the previous dataset.  

proc sql noprint;
  select max(x) format=32. into :lastx trimmed 
  from FILE1
  ;
quit;

data file2;
  retain x &lastx;
  x+1;
  ... rest of data step ...
run;

Or you could make another single observation dataset to store the last value.

Example:

data lastx;
  x=0;
run;
proc print data=lastx; run;

data want lastx(keep=x);
  if _n_=1 then set lastx;
  if eof then output lastx ;
  set sashelp.class(obs=3) end=eof;
  x+1;
  output want;
run;

proc print data=want; run;
proc print data=lastx; run;

data want2 lastx(keep=x);
  if _n_=1 then set lastx;
  if eof then output lastx ;
  set sashelp.class(obs=4) end=eof;
  x+1;
  output want2;
run;

proc print data=want2; run;
proc print data=lastx; run;
Mscarboncopy
Pyrite | Level 9

Thank you! I will let you know after I try it out. Much appreciated.

Mscarboncopy
Pyrite | Level 9

Thank you again @Tom This is exactly what I needed.

hackathon24-white-horiz.png

Join the 2025 SAS Hackathon!

Calling all data scientists and open-source enthusiasts! Want to solve real problems that impact your company or the world? Register to hack by August 31st!

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