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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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