Hello Everyone,
I have 1 old project with project number AND a new project without project number .
As I have to assign new number to new project, I will take the last project number of old file and feed it to the new file and increase by 1.
Look like in the new file, the code is simple like that:
data new; set new;
project_code= &Max_number + _N_;
run;
In my example, the 2 new project should have number: 103, 104.
I dont know how get that &Max_number.
Can anyone please help me?
Thank you.
HHC
data oldproject;
input name :$5. number;
datalines;
ab 1
cd 3
kk 102
;run;
data newproject;
input name :$5. ;
datalines;
new1
new2
;run;
Solution
*take the last record and store the value to Maxnumber;
data last; set oldproject end=eof;
if eof then output;run;
proc sql noprint;
select number into: maxnumber
from last; quit;
data newproject; set newproject;
number = &maxnumber + _n_;
run;
*OR simply use the Max();
proc sql noprint;
select max(number) into: maxnumber
from oldproject;
quit;
Something like this?
proc sql noprint; select max(number) into: maxnumber from oldproject; quit; data newproject; input name :$5. ; number = &maxnumber + _n_; datalines; new1 new2 ;run;
Of course if you want the LAST value not the largest value of number from Oldproject you'll need different code.
And what's the expected output?
Something like this?
proc sql noprint; select max(number) into: maxnumber from oldproject; quit; data newproject; input name :$5. ; number = &maxnumber + _n_; datalines; new1 new2 ;run;
Of course if you want the LAST value not the largest value of number from Oldproject you'll need different code.
This is amazing code with proc SQL!
The DATA NEWPROJECT step can be modified to fetch the maximum NUMBER value from oldproject:
data oldproject;
input name :$5. number;
datalines;
ab 1
cd 3
kk 102
;run;
data newproject (drop=_:);
input name :$5. ;
if _n_=1 then do until (old_end);
set oldproject (keep=number rename=(number=_old_number)) end=old_end;
number=max(number,_old_number);
end;
number+1;
datalines;
new1
new2
;run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.