How to add in SAS code record counts when creating a header for XML data file?

Hello everyone,
I am trying to create XML record counts in the XML data file header.
Below code creates a XML data file "FinalXMLDataFile" from a SAS dataset "NewData" and the code works fine except counts are not working at all. Current record counts "previousCount" creates "0" counts in the XML Header in the counts section.  

Please let me know where the issue with record counts might be?

libname   mydir '/mydirectory'; 
filename outxml  '/mydirectory/FinalXMLDataFile.xml';

/* Macro variables to provide appropriate tab spacing */
%let tab="   ";
%let tab2="      ";
%let tab3="         ";
%let tab4="            ";
%let previousCount = 0; 


/*XML file information and headers*/
data _null_;
    file outxml;

if _N_ = 1 then
    do;
    
    put '<?xml version="1.0" encoding="UTF-8"?>';
    put '<XMLFile >';	
    put &tab. '<Header>';  
    put &tab2. "<CaseCount>%trim(&previousCount)</CaseCount>";     /* get record count of all XML records */ 
    put &tab2. '<Year>2017</Year>';
    put &tab. '</Header>';
    put &tab. '<Payload>';
    end;
run;

/* Using SAS dataset to create similar XML data file */
data _null_;
    file outxml mod;
    set mydir.NewData NOBS=lst;

    record = _N_ + &previousCount;
    
    if _n_=1 then do;
        put &tab2. '<MasterFile>'; 
    end;

    /*All Data records */
    put &tab3. '<AllData>'; 
    put &tab4. '<unit:VAR1>' VAR1+(-1)'</unit:VAR1>';
    put &tab4. '<unit:VAR2>' VAR2+(-1)'</unit:VAR2>';
    put &tab4. '<unit:VAR3>' VAR3+(-1)'</unit:VAR3>';
    put &tab4. '<unit:VAR4>' VAR4+(-1)'</unit:VAR4>';
 
     call symput("previousCount", _N_ + &previousCount); 
    put &tab3. '</AllData>';  

    if _n_=lst then do;
       put &tab2. '</MasterFile>';  
    end;
run;

/* XML File End*/
data _null_;
    file outxml mod;
    
     put &tab'</Payload>';
     put '</XMLFile>';
run; 

Open in new window



Any suggestions or comments will be appreciated!
labradorchikAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IanStatisticianCommented:
/* EE29065154.sas */

libname   mydir "&basePath/examples"; 
filename outxml  "&basePath/examples/FinalXMLDataFile.xml";
***
* Manufacture a data file to use in example 
***;

data myDir.NewData;

	set	sashelp.cars;
	Var1 = Cylinders;
	Var2 = Weight;
	Var3 = Wheelbase;
	Var4 = Length;
run;


***
* Now start the example
***;


/* Macro variables to provide appropriate tab spacing */
%let tab="   ";
%let tab2="      ";
%let tab3="         ";
%let tab4="            ";

proc SQL noprint;

	select	count(*)
	into	:rowCount
	from	myDir.newData;
quit;


/*XML file information and headers*/
data _null_;
    file outxml;

    put '<?xml version="1.0" encoding="UTF-8"?>';
    put '<XMLFile xmlns:unit="http://labradorchik.com/unit/records/">';	
    put &tab. '<Header>';  
    put &tab2. "<CaseCount>%trim(&rowCount)</CaseCount>";     /* get record count of all XML records */ 
    put &tab2. '<Year>2017</Year>';
    put &tab. '</Header>';
    put &tab. '<Payload>';

run;

/* Using SAS dataset to create similar XML data file */
data _null_;
    file outxml mod;
    set mydir.NewData end=last;

    
    if _N_=1 then put &tab2. '<MasterFile>'; 

    /*All Data records */
    put &tab3. '<AllData>'; 
    put &tab4. '<unit:VAR1>' VAR1+(-1)'</unit:VAR1>';
    put &tab4. '<unit:VAR2>' VAR2+(-1)'</unit:VAR2>';
    put &tab4. '<unit:VAR3>' VAR3+(-1)'</unit:VAR3>';
    put &tab4. '<unit:VAR4>' VAR4+(-1)'</unit:VAR4>';
    put &tab3. '</AllData>';  

    if last then put &tab2. '</MasterFile>';

run;

/* XML File End*/
data _null_;
    file outxml mod;
    
    put &tab'</Payload>';
    put '</XMLFile>';
run;

filename outxml clear;
libname mydir clear;

Open in new window



===================================

Notes
  1. Your XML is incorrect - you have used undeclared namespace for "unit".  I have put in a declaration which works.
  2. The macro variable "PreviousCount" was used in one of your earlier EE questions where you were combining datasets into the one XML file.  In this case you are not doing that so I have renamed the macro variable to be "rowCount" to better show what is going on.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
labradorchikAuthor Commented:
Thank you very much Ian!!
I just tested everything and I get all counts in the XML data file as I should!

Back to your notes:
1. Yes, I forgot to place that like of code in my question code but I do have "unit" in my original code. Good catch!
2. After some testing I finally got the logic of counts in the SAS code for XML data file, so I should be good with counts from now on.

Thank you again for all your suggestions and comments!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Statistical Analysis System (SAS)

From novice to tech pro — start learning today.