Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append datasets and keep variable lengths using the parmbuff option

Status
Not open for further replies.

Jubinell

Technical User
Oct 21, 2008
14
0
0
JP
Hi all,

I found a macro that appends one dataset to the bottom of another and keeps the greater variable length between the 2 sets of variables:


I'm trying to adapt this macro so that the macro accepts as many input dataset names as I give it and outputs to the last dataset name given using the parmbuff option:


Here is my code thus far:

Code:
%macro union /parmbuff; *allows for varying number of input dataset names. The last name is always the output dataset.;
%let num = 1; *initializes name counter;
%let indsname = %scan(&syspbuff,&num); *scans the first dataset name;
%let outdsname = %scan(&syspbuff,-1);  *scans the last (master/output) dataset name;
%let num=%eval(&num+1); *increments name counter from 1 to 2;

data &outdsname; set &indsname; run; *adds the first dataset to the currently empty master dataset;

%do %until (&indsname = &outdsname); *repeats loop for as many times as there are input dataset names;
	proc contents data=&outdsname noprint out=out1(keep=name type length where=(type=2)); *gets lengths;
	proc contents data=&indsname noprint out=out2(keep=name type length where=(type=2));
	run;
	data _null_; *merges current and master dataset;
		file "combined.sas";
		merge out1 out2(rename=(length=length2)) end=last;
		by name;
		if _n_ = 1 then put "Data &outdsname;";
		l = max(length,length2); *gets the longer length for each variable;
		put "   length " name " $ " l 2. ";";
		if last then do;
			put "   set &outdsname &indsname;";
			put "run;";
		end;
	run;
	%include "combined.sas";
	%let num = %eval(&num+1); *increments name counter by 1 and rerun loop;
	%let indsname = %scan(&syspbuff,&num); *scans the next dataset name;
%end;
%mend union;

I get the following error while running %union(test1, test2, test) on existing datasets test1 and test2:

Code:
ERROR:  Read Access Violation In Task [ CONTENTS )
Exception occurred at (679839DF)
Task Traceback
Address   Frame     (DBGHELP API Version 4.0 rev 5)
679839DF  05C7E6D8  sasxkern:mcn_main+0x329DF
679036EC  05C7EDC4  sasyh:mcn_main+0x126EC
678FE367  05C7F3BC  sasyh:mcn_main+0xD367
678F20E8  05C7F5DC  sasyh:mcn_main+0x10E8
671B1626  05C7FB48  sasyoio:mcn_main+0x10626
671AF417  05C7FC88  sasyoio:mcn_main+0xE417
6786B59A  05C7FCC0  sasxshel:mcn_main+0x2A59A
67867E62  05C7FDB4  sasxshel:mcn_main+0x26E62
67871D03  05C7FE44  sasxshel:mcn_main+0x30D03
67871381  05C7FE54  sasxshel:mcn_main+0x30381
67872285  05C7FEB4  sasxshel:mcn_main+0x31285
670FE60F  05C7FEF0  sasqutil:mcn_main+0x3D60F
670C1449  05C7FF88  sasqutil:mcn_main+0x449
01272B02  05C7FFA0  sashost:Main+0xBE72
01276C20  05C7FFB4  sashost:Main+0xFF90
7C80B683  05C7FFEC  kernel32:GetModuleFileNameA+0x1B4

Any ideas on how to make this code work?
 
First up, submit this line before running the macro:-
Code:
options mprint mlogic symbolgen;
You probably already know this, but this will allow you to see how the macro is progressing in the log. It'll help answer my first question, which is "Where does it fail?"

An alternative method to using the PARMBUFF option, which I admit I'm not familiar with and will now have to go look up, is to have your datasets listed in 1 parameter (ie spaces between them rather than commas), then use the %scan to access the individual dataset names from that list, in basically the same way you are already doing.
Looking at the code there, I'd say you're advanced enough to work out how to do it. :)

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Hmm....strange. I restarted sas, applied the options and reran the code and it ran just fine. (-: Thank you!
 
Ah! Yes, this does sometimes happen. I've had a similar thing several times, I should have thought of that. You would have found that any ode you submitted would have generated a similar error message. Once SAS hangs like that, you need to restart it.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top