DATA Step, Macro, Functions and more

Delete the empty space in XML file

Accepted Solution Solved
Reply
Super Contributor
Posts: 625
Accepted Solution

Delete the empty space in XML file

[ Edited ]
 

Accepted Solutions
Solution
‎03-22-2018 04:16 AM
Super User
Super User
Posts: 7,933

Re: Delete the empty space in XML file

Of course you are getting two lines. You have two PUT statements.  One unconditional and then a second that only writes when the value is not missing.  So change:

put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
if dpf_base eq . then do;
  put '    <dynamicPricingFactor></dynamicPricingFactor>';
end;

to something like

if missing (dpf_base) then do;
  put '    <dynamicPricingFactor></dynamicPricingFactor>';
end;
else do;
  put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
end;

Note that for most XML uses you can use an abbreviated syntax in the XML file for the empty tags.  Like this:

  put '    <dynamicPricingFactor />';

 

 

View solution in original post


All Replies
New Contributor
Posts: 4

Re: Delete the empty space in XML file

Hi,

 

Something like this would solve the issue:

 

 

if dpf_base eq "" then do;

put '    <dynamicPricingFactor></dynamicPricingFactor>';
end;
else do;
put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
end;

 

 

Or are you looking for something else?

 

Best Regards,

Joao Moreira.

Super Contributor
Posts: 625

Re: Delete the empty space in XML file

Posted in reply to joaomoreira_kpmg_com

It´s not working as in the output XML file I see as <dynamicPricingFactor>.</dynamicPricingFactor>. I placed the following code before the code snippet which I gave in the initial post. Appreciate if could help me further.

 

	if dpf_base eq "" then do;

put '    <dynamicPricingFactor></dynamicPricingFactor>';
end;
else do;
put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>
New Contributor
Posts: 4

Re: Delete the empty space in XML file

[ Edited ]

I assume that's because dpf_base is numeric. Is that rigth?

 

If so the code should be:

 

if dpf_base eq . then do;
put '    <dynamicPricingFactor></dynamicPricingFactor>';
end;
else do;
put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
end;

Best Regards,

Joao Moreira

 

EDIT (@Reeza) correct a double quote to single quote.

Respected Advisor
Posts: 4,674

Re: Delete the empty space in XML file

[ Edited ]
Posted in reply to joaomoreira_kpmg_com

or you use the missing() function which works for both numeric and character

if missing(dpf_base eq) then do;

 

Instead of writing this XML using data step put statements it might also be worth testing if the libname XMLV2 engine could generate an acceptable result.

libname test xmlv2 'c:\test\test.xml';
data test.class;
  set sashelp.class;
  if _n_=2 then call missing(name);
run;
Super Contributor
Posts: 625

Re: Delete the empty space in XML file

Could you please help me to resolve this error?

 

60         		if missing(dpf_base eq) then
                                   _
                                   390
                                   76
ERROR 390-185: Expecting an relational or arithmetic operator.

ERROR 76-322: Syntax error, statement will be ignored.

61         do;
62         put '    <dynamicPricingFactor></dynamicPricingFactor>';
63         end;
           ___
           161
ERROR 161-185: No matching DO/SELECT statement.
Super User
Super User
Posts: 7,933

Re: Delete the empty space in XML file

The MISSING() function just takes a value.  The EQ keyword does not make sense there.

if missing(dpf_base) then
Super Contributor
Posts: 625

Re: Delete the empty space in XML file

I think the tricky part is where we adding that if clause. Could you please
help me understand where should I add the If clause? Is it at the
beginning of the code or in between the code which I shared in the initial
post?
Super Contributor
Posts: 625

SAS dataset to XML converion

I have the placed below the snippet of SAS program  which will convert the SAS dataset to XML file. However when I do, the field dpf_base is not filled in in the SAS dataset but the field <dynamicPricingFactor> in the xml file is filled in with a ‘.’  instead of leaving it as empty. Appreciate if someone of you guide me to resolve this issue.

 

put '<dynamicPricingFactorPolicyCover>';
	put   '<productVersionCoverRef>';
	put '    <externalIdentifier>'  cover +(-1) '</externalIdentifier>';
	put '   </productVersionCoverRef>';
	put '    <isForNonMatch>false</isForNonMatch>';
	put '    <hashCode1>'  veh_driver_hashcode +(-1)  '</hashCode1>';
	put '    <hashCode2>' version_subversion_hashcode +(-1) '</hashCode2>';
	put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
	put '    <dynamicPricingFactorPolicyCoverLangList>';
	put '       <dynamicPricingFactorPolicyCoverLang>';
	put '            <languageRef>';
	put '                 <externalIdentifier>FR</externalIdentifier>';
	put '            </languageRef>';
	a=translate(expl_base,'-','/');
	put '            <explanation>' a '</explanation>';
	put '        </dynamicPricingFactorPolicyCoverLang>';
	put '        <dynamicPricingFactorPolicyCoverLang>';
	put '            <languageRef>';
	put '                 <externalIdentifier>VLS</externalIdentifier>';
	put '            </languageRef>';
	put '            <explanation>' a '</explanation>';

	*put '            <explanation>' translate(expl_base,'-','/') '</explanation>';
	put '        </dynamicPricingFactorPolicyCoverLang>';
	put '    </dynamicPricingFactorPolicyCoverLangList>';
	put ' </dynamicPricingFactorPolicyCover>';
Super User
Posts: 23,293

Re: SAS dataset to XML converion

Set option missing=‘’;

or add an IF statement to conditionally put the value or missing

or use COALESCE to determine the actual value to handle the missing value.

 


@Babloo wrote:

I have the placed below the snippet of SAS program  which will convert the SAS dataset to XML file. However when I do, the field dpf_base is not filled in in the SAS dataset but the field <dynamicPricingFactor> in the xml file is filled in with a ‘.’  instead of leaving it as empty. Appreciate if someone of you guide me to resolve this issue.

 

put '<dynamicPricingFactorPolicyCover>';
	put   '<productVersionCoverRef>';
	put '    <externalIdentifier>'  cover +(-1) '</externalIdentifier>';
	put '   </productVersionCoverRef>';
	put '    <isForNonMatch>false</isForNonMatch>';
	put '    <hashCode1>'  veh_driver_hashcode +(-1)  '</hashCode1>';
	put '    <hashCode2>' version_subversion_hashcode +(-1) '</hashCode2>';
	put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
	put '    <dynamicPricingFactorPolicyCoverLangList>';
	put '       <dynamicPricingFactorPolicyCoverLang>';
	put '            <languageRef>';
	put '                 <externalIdentifier>FR</externalIdentifier>';
	put '            </languageRef>';
	a=translate(expl_base,'-','/');
	put '            <explanation>' a '</explanation>';
	put '        </dynamicPricingFactorPolicyCoverLang>';
	put '        <dynamicPricingFactorPolicyCoverLang>';
	put '            <languageRef>';
	put '                 <externalIdentifier>VLS</externalIdentifier>';
	put '            </languageRef>';
	put '            <explanation>' a '</explanation>';

	*put '            <explanation>' translate(expl_base,'-','/') '</explanation>';
	put '        </dynamicPricingFactorPolicyCoverLang>';
	put '    </dynamicPricingFactorPolicyCoverLangList>';
	put ' </dynamicPricingFactorPolicyCover>';

 

Super User
Posts: 23,293

Re: Delete the empty space in XML file

You add the clause where you output the statement. 

 

IF <> then do;

put ...

end;

else do;

put ...

end;

 


@Babloo wrote:
I think the tricky part is where we adding that if clause. Could you please
help me understand where should I add the If clause? Is it at the
beginning of the code or in between the code which I shared in the initial
post?



Super Contributor
Posts: 625

Re: Delete the empty space in XML file

Posted in reply to joaomoreira_kpmg_com

Still the issue seem to be not resolved. I just compared the output after (right)  and before (left) updating the code and screenshot below for quick reference. I´m getting two extra lines (red highlighted in right side) as you seen below.

 

Code which I added is,

 

if dpf_base eq . then do;
put '    <dynamicPricingFactor></dynamicPricingFactor>';
end;

Output Comparision:

 

Comparison.PNG

Respected Advisor
Posts: 4,674

Re: Delete the empty space in XML file

@Babloo

Please post sample data for your code so we can provide an actually tested solution.

PROC Star
Posts: 2,316

Re: Delete the empty space in XML file

[ Edited ]

Additional lines make no sense.

You replace one put statement with another, so you'll have the same number of lines.

 

if missing(dpf_base) then 
  put '    <dynamicPricingFactor></dynamicPricingFactor>';
else 
  put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>";
 
  instead of 

  

put '    <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>";

 

<sorry for the spam, the formatting keeps getting butchered.>

Super User
Posts: 23,293

Re: Delete the empty space in XML file

SAS OPTIONS go outside of the PROC. Put the option before the data step and it should work fine. 

 

Im assuming you have data step code somewhere since none is shown. 

 

☑ This topic is solved.

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

Discussion stats
  • 34 replies
  • 208 views
  • 14 likes
  • 6 in conversation