BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

4 REPLIES 4
Reeza
Super User

And what's the expected output?

ballardw
Super User

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.

 

hhchenfx
Barite | Level 11

This is amazing code with proc SQL!

 

mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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