DATA Step, Macro, Functions and more

Store max value in 1 file to use in other file

Accepted Solution Solved
Reply
Super Contributor
Posts: 402
Accepted Solution

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.

 

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;

 


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 11,134

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.

 

View solution in original post


All Replies
Super User
Posts: 19,167

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

And what's the expected output?

Solution
2 weeks ago
Super User
Posts: 11,134

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: 402

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

This is amazing code with proc SQL!

 

Valued Guide
Posts: 947

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 136 views
  • 2 likes
  • 4 in conversation