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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

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.

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User

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;
Crubal
Quartz | Level 8
Thanks!
LinusH
Tourmaline | Level 20
1) What I think you are looking for an automatic constraint. Unfortunately it's not supported by SAS engines. You need to look up the existing max value first.
2) If you want to assign a NULL/MISSING value to a numerical variable, use a dot (as seen in the data step by @Kurt_Bremser). You must always place something to the right of the equal sign in SAS syntax.
Data never sleeps
Crubal
Quartz | Level 8

Thank you and that is the point.

s_lassen
Meteorite | Level 14

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 5 replies
  • 3323 views
  • 1 like
  • 4 in conversation