BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Shantaram
Calcite | Level 5

 

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Oligolas
Barite | Level 11

@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 -

View solution in original post

10 REPLIES 10
AMSAS
SAS Super FREQ

@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:

 

  1. Move your macro definition (%macro file_loop(DC_ID); ... %mend) out side of the data step 
  2. Move the option statements (option mprint ; etc.) out side of the data step 

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)

 

Shantaram
Calcite | Level 5
Hi AMSAS,

put '<formQuestionRef>';
put '<externalIdentifier>Policyno</externalIdentifier>';
put '</formQuestionRef>';
put '<answer>';
put '<value>' DC_01 +(-1) '</value>';
put '</answer>';
EX. policy_no is a question and DC_01 is the answer. Suppose policy number is empty(answer value) in the table at that time i need </Value> not like <value> </value> output.
Oligolas
Barite | Level 11

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
Calcite | Level 5
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.
Oligolas
Barite | Level 11

@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
Calcite | Level 5
Hi Oligolas,

Thank you so much.
Its working.
Tom
Super User Tom
Super User

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
Calcite | Level 5
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.
Tom
Super User Tom
Super User

@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/>

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
  • 10 replies
  • 715 views
  • 0 likes
  • 4 in conversation