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.
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 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.