How to pass the value to the file_loop macro inside the XML tag.
Example: '<value>' DC_01 +(-1) '</value>'; i need to pass the value of DC_01 to the file_loop macro.
data _null_;
set transformed_data;
file "$test/DB/TEST_test1.xml";
if _n_=1 then
do;
put '<?xml version="1.0" encoding="UTF-8"?>';
put '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:type="http://www.quinity.com/qis/soap/qisxmlpolicyrequestservice/type">';
put '<soapenv:Header />';
put '<soapenv:Body>';
put '<handlePolicyApplicationRequest>';
put '<policyRequest>';
options mprint;
options mlogic;
options symbolgen;
%macro file_loop(DC_ID);
%if &DC_ID. ne Missing %then %Do;
put '<answer>';
put '<value>' &DC_ID. +(-1) '</value>';
put '</answer>';
%End;
%else %Do;
put '<answer>';
put '<value/>';
put '</answer>';
%end;
%mend;
Put '<applicationForm>';
Put '<applicationFormGroupList>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>PolicyDate</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>Policy</externalIdentifier>';
put '</formQuestionRef>';
put '<answer>';
put '<value>' DC_01 +(-1) '</value>';
/* %file_loop(DC_01);*/
put '</answer>';
put '</applicationFormQuestion>';
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>Prod</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>ProdName</externalIdentifier>';
put '</formQuestionRef>';
put '<answer>';
put '<value>' DC_02 +(-1) '</value>';
/* %file_loop(DC_02;*/
put '</answer>';
put '</applicationFormQuestion>';
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '</applicationFormGroupList>';
put '</applicationForm>';
put '</policyRequest>';
put '</handlePolicyApplicationRequest>';
put '</soapenv:Body>';
put '</soapenv:Envelope>';
end;
run;
@Shantaram wrote:
I have to pass value from (put '<value>' &DC_ID. '+(-1) </value>';) here. instead if &DC_id. i have to pass column value to the macro.
ok, I think what you want is this: (replace -100 / 100 with the value needed
%macro file_loop(DC_ID,addValue);
if &DC_ID. ne . then Do;
put '<answer>';
&DC_ID.=&DC_ID.+&addValue.;
put '<value>' &DC_ID.'</value>';
put '</answer>';
End;
else Do;
put '<answer>';
put '<value/>';
put '</answer>';
end;
%mend file_loop;
options mprint mlogic symbolgen;
data _null_;
set transformed_data;
file "$test/DB/TEST_test1.xml";
if _n_=1 then
do;
put '<?xml version="1.0" encoding="UTF-8"?>';
put '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:type="http://www.quinity.com/qis/soap/qisxmlpolicyrequestservice/type">';
put '<soapenv:Header />';
put '<soapenv:Body>';
put '<handlePolicyApplicationRequest>';
put '<policyRequest>';
Put '<applicationForm>';
Put '<applicationFormGroupList>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>PolicyDate</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>Policy</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_01,-100);
put '</applicationFormQuestion>';
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>Prod</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>ProdName</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_02,100);
put '</applicationFormQuestion>';
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '</applicationFormGroupList>';
put '</applicationForm>';
put '</policyRequest>';
put '</handlePolicyApplicationRequest>';
put '</soapenv:Body>';
put '</soapenv:Envelope>';
end;
run;
- Cheers -
@Shantaram
It's not clear to me what you are trying to achieve, can you reword your request maybe with a simple example without any macros
A couple of points to make your code clearer to understand:
When working with macros it is easier if you initially create the code without any macros and test. Then add macro variables (e.g. %let var=5; put "my macro variable value is %var" ; ) and test. Finally create macro definitions (%macro name; %mend)
Hi,
Inside file_loop you need to quote the text +(-1) as well.
put '<value>' &DC_ID. '+(-1) </value>';
If you want to check whether a dataset variable has been passed to %file_loop then check vs nothing like:
%if &DC_ID. ne %then %Do;
%macro file_loop(DC_ID);
%if &DC_ID. ne %then %Do;
put '<answer>';
put '<value>' &DC_ID. '+(-1) </value>';
put '</answer>';
%End;
%else %Do;
put '<answer>';
put '<value/>';
put '</answer>';
%end;
%mend;
- Cheers -
@Shantaram wrote:
I have to pass value from (put '<value>' &DC_ID. '+(-1) </value>';) here. instead if &DC_id. i have to pass column value to the macro.
ok, I think what you want is this: (replace -100 / 100 with the value needed
%macro file_loop(DC_ID,addValue);
if &DC_ID. ne . then Do;
put '<answer>';
&DC_ID.=&DC_ID.+&addValue.;
put '<value>' &DC_ID.'</value>';
put '</answer>';
End;
else Do;
put '<answer>';
put '<value/>';
put '</answer>';
end;
%mend file_loop;
options mprint mlogic symbolgen;
data _null_;
set transformed_data;
file "$test/DB/TEST_test1.xml";
if _n_=1 then
do;
put '<?xml version="1.0" encoding="UTF-8"?>';
put '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:type="http://www.quinity.com/qis/soap/qisxmlpolicyrequestservice/type">';
put '<soapenv:Header />';
put '<soapenv:Body>';
put '<handlePolicyApplicationRequest>';
put '<policyRequest>';
Put '<applicationForm>';
Put '<applicationFormGroupList>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>PolicyDate</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>Policy</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_01,-100);
put '</applicationFormQuestion>';
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>Prod</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>ProdName</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_02,100);
put '</applicationFormQuestion>';
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '</applicationFormGroupList>';
put '</applicationForm>';
put '</policyRequest>';
put '</handlePolicyApplicationRequest>';
put '</soapenv:Body>';
put '</soapenv:Envelope>';
end;
run;
- Cheers -
This question:
How to pass the value to the file_loop macro inside the XML tag.
Example: '<value>' DC_01 +(-1) '</value>'; i need to pass the value of DC_01 to the file_loop macro
Does not make much sense. You pass text to a macro. So you could pass the text DC_01 to the macro and use the macro to generate SAS code that uses the string DC_01 as the name of a variable.
If you want to pass the value of the variable named DC_01 in the source dataset TRANSFORMED_DATA to the text file "$test/DB/TEST_test1.xml" then why would you need any macro code? This PUT statement should do that.
put '<value>' DC_01 +(-1) '</value>';
If you want to pass the value of DC_01 to the macro then that cannot happen in the context this data step. The statements that exist in a data step have to all have been compiled before the data step starts running and so before it can access the values of any variables.
@Shantaram wrote:
Thanks TOM
put '<value>' DC_01 +(-1) '</value>';
Consider their is missing value of DC_01 then it will print <value> </value> but i need to print like this </value> only.
To test if the variable DC_01 has a missing value on this observation use SAS code, not macro code.
if missing(DC_01) then do;
...
end;
else do;
...
end;
Are you sure the user of the XML file cares how you have coded the XML to represent a missing value? These two lines should mean the same thing.
<value></value> <value/>
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.