I created a UID in my data set like so:
data two; set one; UID + 1; run;
the last record is:
name UID
Tom 899
I have a new data set named new_students like so:
name
**bleep**
Harry
etc.
I want to add these records to data set two to get this result:
name UID
Tom 899
**bleep** 900
Harry 901
etc
/*Existing sample with uid*/
data one;
set sashelp.class;
uid+1;
run;
/*Read the last uid and store in a macro var*/
data _null_;
set one nobs=nobs point=nobs;
call symputx('uid',uid,'g');
stop;
run;
/*write to the log to check the value to help debug if any*/
%put &=uid;
/*Increment the counter for two using uid macro var*/
data two;
set sashelp.class;
retain uid &uid;
uid+1;
run;
/*append two to one*/
proc append base=one data=two;
run;
/*Existing sample with uid*/
data one;
set sashelp.class;
uid+1;
run;
/*Read the last uid and store in a macro var*/
data _null_;
set one nobs=nobs point=nobs;
call symputx('uid',uid,'g');
stop;
run;
/*write to the log to check the value to help debug if any*/
%put &=uid;
/*Increment the counter for two using uid macro var*/
data two;
set sashelp.class;
retain uid &uid;
uid+1;
run;
/*append two to one*/
proc append base=one data=two;
run;
If you don't mind using undocumented SAS features:
data oldData;
do name = "Georges", "Henry", "Paul";
UID + 1;
output;
end;
run;
data newData;
do name = "Loretta", "Mary", "Suzy";
output;
end;
run;
proc sql;
select max(UID) into : maxUID from oldData;
insert into oldData (UID, name)
select &maxUID + monotonic(), name from newData;
quit;
proc print data=oldData; run;
Obs. name UID 1 Georges 1 2 Henry 2 3 Paul 3 4 Loretta 4 5 Mary 5 6 Suzy 6
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.