## Store max value in 1 file to use in other file

Solved
Highlighted
Super Contributor
Posts: 458

# Store max value in 1 file to use in other file

[ Edited ]

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.

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;``````

Accepted Solutions
Solution
‎10-06-2017 07:39 PM
Super User
Posts: 12,148

## Re: Store max value in 1 file to use in other file

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.

All Replies
Super User
Posts: 21,530

## Re: Store max value in 1 file to use in other file

And what's the expected output?

Solution
‎10-06-2017 07:39 PM
Super User
Posts: 12,148

## Re: Store max value in 1 file to use in other file

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.

Super Contributor
Posts: 458

## Re: Store max value in 1 file to use in other file

This is amazing code with proc SQL!

Posts: 1,148

## Re: Store max value in 1 file to use in other file

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;
``````
☑ This topic is solved.