Hi, I have a question in proc sql procedure how to insert a row.
Initial table (Table_1) looks like this:
Log_Id | Start_Time | End_Time
-----------------------------------------------------------------------------
29 | 16DEC2017:23:46:59.733390 |
-----------------------------------------------------------------------------
30 | 16DEC2017:23:47:23.833490 |
I want to insert a row where 'Log_Id' is a sequential number to prior ones (31 in this example); and 'Start_Time' is current time. Leave 'End_Time' blank.
My code:
proc sql;
insert into Table_1
set Log_id = 31
, Start_Time = datetime()
, End_Time =
;
quit;
1) How to make 'Log_Id' a sequential number to prior rows?
2) It has error if I leave 'End_Time' blank. And that column is of datetime format.
Thank you so much.
I think this will work:
proc sql noprint; select max(Log_id)+1 into :Log_id from Table_1; insert into Table_1 set Log_id=&Log_id, Start_Time=datetime(); quit;
You could also do it in one step, but then you would have to use UNDOPOLICY=NONE, as you are inserting into the same table that you are querying for the MAX value. It is not necessary to supply a value for End_Date, it will automatically be set missing.
I'd do it automatically with a data step:
data table_1;
set table_1 end=done;
output;
if done
then do;
log_id + 1;
start_time = datetime();
end_time = .;
output;
end;
run;
Thank you and that is the point.
I think this will work:
proc sql noprint; select max(Log_id)+1 into :Log_id from Table_1; insert into Table_1 set Log_id=&Log_id, Start_Time=datetime(); quit;
You could also do it in one step, but then you would have to use UNDOPOLICY=NONE, as you are inserting into the same table that you are querying for the MAX value. It is not necessary to supply a value for End_Date, it will automatically be set missing.
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.