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?
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;
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;
Thank you! I will let you know after I try it out. Much appreciated.
Thank you again @Tom This is exactly what I needed.
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!
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.
Ready to level-up your skills? Choose your own adventure.