Help using Base SAS procedures

change data step to proc sql procedure

Reply
Regular Contributor
Posts: 154

change data step to proc sql procedure

[ Edited ]

Hi, 

 

I have a question changing this 'data step' procedure to 'proc sql' procedure.

 

Data Step Procedure: ('num_rec_prop' is a parameter that I have set and called)

 

data Process_Step_Error_Log;
		if &num_rec_prop NE 1 then do;
			Step_ID = 001;
			Error_Code = 'AEE001';
		end;

		else do;
			Step_ID = 002;
			Error_Code = 'SUC001';
		end;
                output;
run;

 

However, when I change this procedure into 'proc sql' and uses macro function. The reason is if there is an error, another table needs to be updated as well (PROC_STEP_LOG, and this table exists initially)

 

I create an empty table first:

	data Proc_Step_Error_Log;
		input Step_Id 10 Error_Code $10;
		datalines;
	run;

Then, I use the following macro procedure: (proc_log_id is read and called before as well) 

%macro etl_error_log;
		%if &num_rec_prop NE 1 
			%then %do;
			insert into Proc_Step_Error_Log
				set Step_Id = 001
				  , Error_Code = "AEE001";

                          update PROC_STEP_LOG
				set 
					proc_step_end_ts = datetime()
					, proc_step_stat_cd = "Failed"
					, lst_updt_ts = datetime()
				where proc_log_id = &proc_log_id;
			%end;

                 %else %do;
			insert into Proc_Step_Error_Log
				set Step_Id = 002
				  , Error_Code = "SUC001";
			%end;
%mend;

%etl_error_log;

 

If I ran this part, error message shows up:

ERROR 180-322: Statement is not valid or it is used out of proper order. (Highlight on the word 'insert')

 

How could I fix my code to run successfully?

 

Thank you! 

Super User
Posts: 10,215

Re: change data step to proc sql procedure

I see no proc sql statement, and no corresponding quit; to end the proc sql.

Strong hint: always get your code to run without macro action before making it dynamic.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 154

Re: change data step to proc sql procedure

Posted in reply to KurtBremser

Thanks @KurtBremser

 

Absolutely right. It can work after adding this syntax. 

Super User
Super User
Posts: 9,599

Re: change data step to proc sql procedure

There really is no need for any macro code here.  Why do you need to change this into SQL, are you connecting to a database?  You can do it simply with a case:

proc sql;
  insert into etl_error_log
  set  stepid=case when &num_rec_prop. ne 1 then "001" else "002" end,
       error_code=case when &num_rec_prop. ne 1 then "AEE001" else "SUC001" end;
quit;

However that being said, I would question why you are doing this in the first place.  What is the process, how are you getting the data in?  

Regular Contributor
Posts: 154

Re: change data step to proc sql procedure

@RW9 Thanks for your reply!

The reason I am changing the data procedure to proc sql is because the tables I try to update is in the Oracle. 

 

 

Super User
Super User
Posts: 9,599

Re: change data step to proc sql procedure

Well, step 1 is deciding where to do the work then.  There is no point in manipulating tables on a database from SAS, either download the data to SAS, process, then upload again, or do the processing on the database.  All your doing is hitting the large overhead of data transmittal back and forth.  If you do this on the database for instance you can set hooks on your dataset, which, when a condition is struck, inserts data automatically into the journal tables.  I would also question why the need for "Error" codes in the first place.  SAS is a data driven language, if no data is read in then the whole process can run and there would be no output, no need to generate an error system to catch these things as the code should run either way.

Regular Contributor
Posts: 154

Re: change data step to proc sql procedure

Thank you @RW9

 

The first step I am doing (create an empty table for Proc_Step_Error_Log) is because it does not exist in the Oracle database initially. So that I want to add this part to avoid error. 

 

Once the table exists, I will delete step 1 procedure. 

Ask a Question
Discussion stats
  • 6 replies
  • 224 views
  • 0 likes
  • 3 in conversation