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

Hi,

 

I can't figure out why this code is not working.  I am trying to insert dummy rows into this data set when there are no observations found on it.  If I run the proc sql alone, it will insert the rows.  However, when I use the call execute if noobs=0, no observations are inserted.

 

data _null_;
set msrflow.msrflow_loansfunded_10YR_append;
if nobs=0 then do;
call execute(
'proc sql; 
	insert into msrflow.msrflow_loansfunded_10YR_append 
			(
				PRIMARY_LOAN_KEY 
				,INVESTOR_LOAN_ID 
				,SELLER_LOAN_NUMBER 
				,PNC_LOAN_NUMBER 
				,STATUS 
				,INVESTOR 
				,FUNDING_DATE 
				,NOTE_BALANCE 
				,NOTE_RATE 
				,SERVICE_FEE 
				,LTV 
				,FICO 
				,SRP_RATE 
				,SRP_AMOUNT 
				,PRODUCT 
				,ESCROW_FLAG 
				,PURPOSE 
				,TIMEFRAME 
				,PCT_TOTAL_UPB_PURCHASE 
				,PCT_TOTAL_UPB_REFI 
				,PCT_TOTAL_CNT_ESCROW 
				,PCT_TOTAL_CNT_NONESCROW 
				,WGT_AVG_10YR 
			)
		values (0,"","","","","",.,0,0,0,0,0,0,0,"","","","MTD",0,0,0,0,0)
		values (0,"","","","","",.,0,0,0,0,0,0,0,"","","","YTD",0,0,0,0,0)
		values (0,"","","","","",.,0,0,0,0,0,0,0,"","","","LTD",0,0,0,0,0)
	;
quit;'
);
end;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

When a set statement attempts to read beyond the end of a data set, the data step immediately stops.   With zero obs, the first SET provokes that response.   There is a way, however, to get the proper nobs:

 

data _null_;
  if 0 then set msrflow.msrflow_loansfunded_10YR_append nobs=nrecs;
  put nrecs=;
  if nrecs=0 then call execute ('....');
  stop;
run;

 

This works because:

  1. The is no attempt to read beyond the end of data  ("if 0" is always false).
  2. But the "nobs=..." option is evaluated prior to data reading, so NRECS will have a testable value.
  3. The "stop" statement is not necessary, but a good habit for cases like this.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

Maxim 2: read the log. You'll find a NOTE that nobs is uninitialized. You forgot the nobs= option in the set statement, and you need to move the set statement after the end and before the run.

mkeintz
PROC Star

When a set statement attempts to read beyond the end of a data set, the data step immediately stops.   With zero obs, the first SET provokes that response.   There is a way, however, to get the proper nobs:

 

data _null_;
  if 0 then set msrflow.msrflow_loansfunded_10YR_append nobs=nrecs;
  put nrecs=;
  if nrecs=0 then call execute ('....');
  stop;
run;

 

This works because:

  1. The is no attempt to read beyond the end of data  ("if 0" is always false).
  2. But the "nobs=..." option is evaluated prior to data reading, so NRECS will have a testable value.
  3. The "stop" statement is not necessary, but a good habit for cases like this.
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
elwayfan446
Barite | Level 11

Thank you both for the help.  Both solutions worked for me and were very helpful.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 3 replies
  • 1013 views
  • 0 likes
  • 3 in conversation