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

Hello,
I want to create an xml file from a SAS table with a hierarchy. 
The table has the following 8 fields:
atmld, bankcode, bankbranchcode, schum, dt, monthreport, cuerrentdate, factorcode.

 

Example table:

atmldbankcodebankbranchcodeschumdtmonthreportcuerrentdatefactorcode
S$9821260012342020-09-122020-022019-05-3012
S$98212600123422020-09-132020-022019-05-3012


Example of how I would like the xml file to be displayed.

shlomiohana_0-1635190878996.jpeg

This is the code I wrote:

data _null_;
	set work.report end=eof;
	file "&outfile./Report.xml" noprint encoding="utf-8";

	if _n_ =1 then
		do;
			put '<?xml version="1.0" encoding="utf-8"?>';
			put '<ATMstockReport>';
			put '<currentDate>' currentDate '</currentDate>';
			put '<factorCode>' factorCode '</factorCode>';
			put '<monthReport>' monthReport '</monthReport>';
			put '<atmsStock>';
		end;

	put '<atm atmId="'atmId'">';
	put '<bankCode>' bankCode '</bankCode>';
	put '<bankBranchCode>' bankBranchCode '</bankBranchCode>';
	put '<dates>';
	put '<date dt="'cdt'">';
	put '<schum>' schum '</schum>';
	put '</date>';
	put '</dates>';
	put '</atm>';

	if eofl then
		do;
			put '</atmsStock>';
			put '</ATMstockReport>';
		end;
run;

How do I fix the code so that the 2 dates or more are open under the same atmId as in the picture?

I do not want each record to be opened separately.


Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

Your screenshot is hard to read but something like this should work.

Please adapt.

data _null_;
	set work.report end=eof;
	file "&outfile./Report.xml" noprint encoding="utf-8";
by ATMID BANKCODE BANKBRANCHCODE ;
	if _n_ =1 then
		do;
			put '<?xml version="1.0" encoding="utf-8"?>';
			put '<ATMstockReport>';
			put '<currentDate>' currentDate '</currentDate>';
			put '<factorCode>' factorCode '</factorCode>';
			put '<monthReport>' monthReport '</monthReport>';
			put '<atmsStock>';
		end;
if first.BANKBRANCHCODE then do;
	put '<atm atmId="'atmId'">';
	put '<bankCode>' bankCode '</bankCode>';
	put '<bankBranchCode>' bankBranchCode '</bankBranchCode>';
        put '<dates>'; 
end;
	put '<date dt="'cdt'">';
	put '<schum>' schum '</schum>';
	put '</date>';
if last.BANKBRANCHCODE then do;
	put '</dates>';
	put '</atm>';
end;
	if eofl then
		do;
			put '</atmsStock>';
			put '</ATMstockReport>';
		end;
run;

Note that the data must be sorted or indexed.

View solution in original post

1 REPLY 1
ChrisNZ
Tourmaline | Level 20

Your screenshot is hard to read but something like this should work.

Please adapt.

data _null_;
	set work.report end=eof;
	file "&outfile./Report.xml" noprint encoding="utf-8";
by ATMID BANKCODE BANKBRANCHCODE ;
	if _n_ =1 then
		do;
			put '<?xml version="1.0" encoding="utf-8"?>';
			put '<ATMstockReport>';
			put '<currentDate>' currentDate '</currentDate>';
			put '<factorCode>' factorCode '</factorCode>';
			put '<monthReport>' monthReport '</monthReport>';
			put '<atmsStock>';
		end;
if first.BANKBRANCHCODE then do;
	put '<atm atmId="'atmId'">';
	put '<bankCode>' bankCode '</bankCode>';
	put '<bankBranchCode>' bankBranchCode '</bankBranchCode>';
        put '<dates>'; 
end;
	put '<date dt="'cdt'">';
	put '<schum>' schum '</schum>';
	put '</date>';
if last.BANKBRANCHCODE then do;
	put '</dates>';
	put '</atm>';
end;
	if eofl then
		do;
			put '</atmsStock>';
			put '</ATMstockReport>';
		end;
run;

Note that the data must be sorted or indexed.

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
  • 1 reply
  • 739 views
  • 2 likes
  • 2 in conversation