BookmarkSubscribeRSS Feed
dennis_oz
Quartz | Level 8

 

/******************/
/* Dataset         */
/******************/
%Macro ImportData_BABC(result_file,output_dataset);
	/*	%let filename=%sysfunc(catx(\,&path1,&result_file));*/
	%let filename=%sysfunc(catx(\,&path1,&file_name));
	%put 'entered loop   ImportData file_name'    &filename.;

	/*	%let file_name_new=%sysfunc("'"||STRIP('&filename')||"'");*/
	/*	%put 'entered loop - file_name_new  '    &file_name_new.;*/
	/*	data _null_;*/
	/*		call symput("file_name_new","'"||STRIP(&filename)||"'");*/
	/*		call symput ('file_name_new',"'"||STRIP('&filename')||"'");*/
	/*	run;*/
	/*	%put 'entered loop - file_name_new  '    &file_name_new.;*/
	%if %sysfunc(fileexist(&filename)) %then
		%do;
			filename holdit  "In\Temp\Hold\test1.txt" lrecl=32766;

			/*			filename in_file   pipe %sysfunc(quote(dir "&filename." /b));*/
			filename in_file pipe "dir ""&filename."" /b";

			/* Reading in the .csv file and creating a temp .txt file                                                        */
			/* After Writing out the first row with Column Names will write the second row with '*'(asterix)    */
			/* Asterix will be equal to the number of column headers present in File                            */
			/* Open filename 'holdit' (test1.txt) */
			/* Action: Need to add a step to remove the strange character that appears within the text. Will do that on return from vac*/
			data _null_;
				length string $4000;
				file holdit LRECL=32767;
				infile "&filename" lrecl=32767 end = done;
				input;
				put _infile_;
				wcount = countw(_infile_,',');
				string =('*'||repeat(',*',wcount-2));
				put string;

				do until(done);
					input;
					put _infile_;
				end;

				stop;
			run;

			/* this step release the input file */
			filename in_file;
			%put '**********  1   ***';

			proc import
				datafile=holdit out=work.temp DBMS=CSV replace;
				/*   datafile="&path1\&rtcf_file." out=work.&output_file. dbms=xlsx replace;*/
				guessingrows=444;
				getnames=yes;
			run;

			/* Setting all the Variable to a default var length of 4000 */
			filename tmp "\Temp\Hold\test3.txt" lrecl=32766;
			filename holdit;

			data _null_;
				set work.temp  end=eod;
				array chrvar {*} $1000 _character_;
				array len{1000} _temporary_;

				do c=1 to dim(chrvar);
					len{c}=max(4000,length(chrvar{c}));
				end;

				file tmp;

				if eod then
					do c=1 to dim(chrvar);
						vnam=vname(chrvar{c});
						put vnam '$' len{c};
					end;
			run;

			data work.want;
				length %include tmp ;;
				set work.temp;
			run;

			/*          If table does exist  */
			%if %sysfunc(exist(caciamaz.&output_dataset.)) %then
				%do;

					data work.&output_dataset.;
						format ResultsLoadDate date9.;
						set work.want(firstobs=2);

						/*varname = compress(translate(REASON_FOR_SCORE, "'~~", "’"), '~');*/
						Result_file=symget('file_name');
						ResultsLoadDate="&file_upload_date."d;
					run;

					/*					DATA caciamaz.&output_dataset.;*/
					/*						DO UNTIL (lastrec);*/
					/*							SET caciamaz.&output_dataset. work.&output_dataset. end = lastrec;*/
					/*							OUTPUT;*/
					/*						END;*/
					/**/
					/*					RUN;*/
					data caciamaz.&output_dataset.;
						set caciamaz.&output_dataset. work.&output_dataset.;
					run;

					%put 'helleeeee';

					/*					%sysexec  move "&filename" "&path1\ArchiveProcessed";*/
				%end;
			%else
				%do;
					/*          If table does not exist  */
					data work.&output_dataset.;
						length Result_file $200.;
						format ResultsLoadDate date9.;
						set work.want(firstobs=2);

						/*    varname = compress(translate(REASON_FOR_SCORE, "'~~", "’"), '~');*/
						Result_file=symget('file_name');
						ResultsLoadDate="&file_upload_date."d;
					run;

					data caciamaz.&output_dataset.;
						set work.&output_dataset.;
					run;

				%end;
		%end;
%Mend ImportData_BABC;

File 1
------
%ImportData_BABC('file1.csv',results_hi_babc_med_in)

File 2
------ %ImportData_BABC('file2.csv',results_hi_babc_med_in)

The above code works but when I pass input file to be read  dynamically( when there is more than one file)  the sas session does not exit or stop.

 

this is the code to pass file dynamically

/* Read in all files from FTP In directory */
filename infile pipe "dir "&wash_file." /b";

data input_file;
	length file_name   $200;
	format file_upload_date date9.;
	infile infile truncover;
	input file_name $50.;
	file_upload_date=input(scan((scan(file_name,-1,'_')),1,'.'),YYMMDD10.);
	actual_file_name=upcase(substr(file_name,1,(length(file_name)-length(scan(file_name,-1,'_')))-1));

	/*	call symput('file_upload_date',put(file_upload_date,DATE9.));*/
	call symput ('file_name',file_name);
run;

proc sort data=input_file nodupkey;
	by actual_file_name file_upload_date;
run;

%put &file_upload_date.;
%put &file_name.;

/*Count number of records of filenames in dataset*/
data _null_;
	%global records;
	set input_file end=last;

	if last then call symputx("Records",put(_n_,8.)); /*_n_ is observation number in sas dataset*/
run;

%put &Records.;



/* End Import Files by Stream  */
/* Main Logic - Reading result file based on the stream */
%macro Import_File(records);
	%if  &records. > 0 %then
		%do;
			/* %do i=1 %to &records.; */
			%put 'enterd loop 1';

			data _null_;
				set input_file;
				call symput ('file_name',strip(file_name));

					when ('BABC_EXPORT')                  rc=dosubl('%ImportData_BABC(&file_name,results_hi_babc_med_in)');
					otherwise;
				
				end;

					STOP;

				%put 'exited loop 1';
			run;

		%end;
	%else
		%do;
			%put 'loop   6';

			/*			data _null_;*/
			/*				rc=dosubl('%folder_empty_message()');*/
			/*			run;*/
			%put 'exited loop  6 ';
		%end;
%mend;

thus code was working before when I had the files that had to be read properly declared ie. using prc import I had all the columns defined.

 

But now I want to NOT define columns and read the file.. Hence changed the first part of the code. I am trying to re-use the part that dynamically passes the file but it is not stopping after all the files that have been read. 

 

I need the code to exit gracefully.

 

The logic is working but it just hangs after the files are read. I hope I am making sense.

Thanks.

 

7 REPLIES 7
ballardw
Super User

Pretty obviously that LOG is only part of the log.

 

I think that you may need to describe what you mean by "session does not complete".

 

If you mean not all of the output that you expect was not created then you need to specify what was not created as we have none of your files and can't actually test this code. In fact, you do not even show the PARAMETERS that were provided by you when you run the macro.

 

Suggestion: Set the system options MPRINT and rerun this. The option will show the code generated and then look at the log. You should see if all the stuff you expect executes.

 

You are using a FILE TMP and %include TMP without defining the actual file to use. I suggest explicitly defining it so there is not question as to where the file may be written so you can find it to see if the contents are as expected.

 

 

Kurt_Bremser
Super User

The icon for the code indicates a WARNING. Inspect the log from the top down and fix the issue(s) causing the WARNINGs. Run the code step by step, until you arrive at the macro definition and macro call.

 

It may be that something in your code keeps Enterprise Guide from correctly recognizing that the code has actually finished to run (unbalanced quotes or unbalanced comment indicators can do this)

Astounding
PROC Star
Could your code contain multiple errors?
Here is one. %include must follow a semicolon. It doesn't belong in the middle of a LENGTH statement. You have to redesign the program so that the word LENGTH is actually the first word in your TMP file, and remove the word LENGTH from the DATA step.
Kurt_Bremser
Super User

%INCLUDE is used here in a macro, so it can be used anywhere:

 

  • When used in the DATA step, the %INCLUDE statement must be the first statement or it must immediately follow a semicolon that ends another statement. This restriction does not apply if the %INCLUDE statement is used with the macro facility.

(from the documentation of the %INCLUDE statement)

Astounding
PROC Star
I stand corrected! Good find!
Kurt_Bremser
Super User

After digging into the code, I see it contains a very complicated way to set all character variables to a minimum length of 4000. This can be done much easier:

proc sql noprint;
select
  catx(" ",name,'$',max(length,4000)) into :deflength separated by " "
from dictionary.columns
where libname = "WORK" and memname = "TEMP" and type = "C"
;
quit;

data out (compress=yes);
length &deflength.;
set temp;
run;

It is always a good idea to use COMPRESS=YES for datasets containing lots of long character variables.

AllanBowe
Barite | Level 11

Actually, it can be problematic to gracefully break out of a %include - we often face this problem when building SAS-Powered Web Applications, where it's a common requirement to stop processing and show a message to a user WITHOUT a non-zero return code.

 

The mechanism we use in SASjs development is to "wrap" the %include using this macro:  https://core.sasjs.io/mp__include_8sas.html

 

And then afterwards, call the following to pick up any aborts:

 

%mp_abort(mode=INCLUDE)

Source for the above:  https://core.sasjs.io/mp__abort_8sas.html

/Allan
SAS Challenges - SASensei
MacroCore library for app developers
SAS networking events (BeLux, Germany, UK&I)

Data Workflows, Data Contracts, Data Lineage, Drag & drop excel EUCs to SAS 9 & Viya - Data Controller
DevOps and AppDev on SAS 9 / Viya / Base SAS - SASjs

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 885 views
  • 2 likes
  • 5 in conversation