DATA Step, Macro, Functions and more

creating a root tag for XML

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

creating a root tag for XML

Hi all,

Thanks to help in this community, I was able to extract a super large file from a SQL database and export it into XML format (required by my teacher). I was able to get each row's value (in string) and concatenate them into one file to be read.

Right now I'm trying to get it to be read by a XML reader such as XML Notepad. However, as each row has the same root tag, I need to create a new tag

So right now i have many strings with the same root tag.

<Interview>Row1</Interview>

<Interview>Row2</Interview>

<Interview>Row3</Interview>

 

Once I add a tag before them it can be read, but I would like to be able to just code for it to do so automatically.

to look like this:

<Hi>

<Interview>Row1</Interview>

<Interview>Row2</Interview>

<Interview>Row3</Interview>

</Hi>

 

As for the code I simply just used this:

data Complete;
file alex lrecl=90000;
set Alex.Interview;                   *this library already had all the rows in the database and only has one variable which is the interview                                                         which I want to merge and become one ;

Run;

 

I know the code is basically useless but right now I dont really know what to do... This DOES create one XML file that has all the contents I want in it. But it lacks the tag so XML reader can read it.

Thank you


Accepted Solutions
Solution
‎01-30-2018 11:03 AM
Super User
Super User
Posts: 9,376

Re: creating a root tag for XML

Posted in reply to AlexMoreton

There is nothing in that code you post which an XML file, or in fact any physical file other than a dataset?

 

Maybe you need something like:

data _null_;
  set alex.interview end=last;
  file "want.xml";
  if _n_=1 then do;
    put "<Root>";
    put "<Hi>";
  end;
  tmp=cat("<Interview>",yourstring,"</Interview>");
  put tmp;
  if last then put "</Root>";
run;

But its very hard to tell.  Yo know how to make a well structured XML file I presume, it has a line at the top stating version, encoding etc., then open a tag which is the root, and at end close the root tag...

View solution in original post


All Replies
Solution
‎01-30-2018 11:03 AM
Super User
Super User
Posts: 9,376

Re: creating a root tag for XML

Posted in reply to AlexMoreton

There is nothing in that code you post which an XML file, or in fact any physical file other than a dataset?

 

Maybe you need something like:

data _null_;
  set alex.interview end=last;
  file "want.xml";
  if _n_=1 then do;
    put "<Root>";
    put "<Hi>";
  end;
  tmp=cat("<Interview>",yourstring,"</Interview>");
  put tmp;
  if last then put "</Root>";
run;

But its very hard to tell.  Yo know how to make a well structured XML file I presume, it has a line at the top stating version, encoding etc., then open a tag which is the root, and at end close the root tag...

Contributor
Posts: 30

Re: creating a root tag for XML

Hi RW9, I forgot to add in the FILENAME line in the beginning which would have created it into a xml file.

I know that the XML files have that format in which it has those things at the beginning. Which makes me think, should I try and create the tags in a data step that i set alex.interview and just actually add <Hi> before _INTERVIEW_ and </Hi> after it? would that be a possible way?

Thanks for the help so far!

Super User
Super User
Posts: 9,376

Re: creating a root tag for XML

Posted in reply to AlexMoreton
Contributor
Posts: 30

Re: creating a root tag for XML

Thanks RW9,

output's not really complicated, I just need to add a tag in front and after the end of the total dataset at alex.interview. Then I can export it and it should be readable on XML Notepad etc. It's just right now I don't know how to do so...  

the output is basically the same except it will have a new tag in front and at the end of the dataset. which would be <hi> </hi>. Is this not really a good idea to do so? because basically I have rows with the same tags <interview> </interview>. So I just need to create a new tag to become the new root.

Thanks for the advise though! I'll keep trying because it feels like I'm only one step away from making this (which I can do manually, but I just think there HAS to be a code for this).

Thank you RW9

Contributor
Posts: 30

Re: creating a root tag for XML

Thank you RW9,

I played around your code and I managed to get it to do exactly what I wanted. Thank you

filename alex 'X:\ALEX TESTING\TRY5.xml';

data tags;
set work.complete end=last;
file alex lrecl=90000;
if _n_=1 then do;
put "<Hi>";
end;

put Interview;

if last then put "</Hi>";
run;

I'll accept your answer as the answer but this is just in case you wanted to see. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 90 views
  • 1 like
  • 2 in conversation