Hi All,
I am trying to write if condition inside first if condition.
Sample code.
data transformed_data;
input DC_014 $6. DC_019 $13. DC_035 $9.;
cards;
116312 tosst po&ice
118323 tosst Police Mobile
113244 tosst Police Mobile
115333 tosst Police Mobile
118333 tosst Police Mobile
;
run;
%macro file_loop(DC_ID);
if &DC_ID. not in('.',' ') then Do;
if compress(&DC_ID., '% <>&', 'k') in ('&') then Do;
put '<![CDATA[';
put '<answer>';
put '<value>' &DC_ID. +(-1) '</value>';
put '</answer>';
put ']]>';
End;
else Do;
put '<answer>';
put '<value>' &DC_ID. '</value>';
put '</answer>';
end;
End;
else Do;
put '<answer>';
put '<value/>';
put '</answer>';
end;
%mend file_loop;
options mprint mlogic symbolgen;
data _null_;
set transformed_data;
file "c:/TEST_Shan1.xml";
if _n_=1 then
do;
put '<?xml version="1.0" encoding="iso-8859-1"?>';
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>';
/*Code*/
Put '<applicationForm>';
Put '<applicationFormGroupList>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>polGenStartdate</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>Pol1</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_014);
put '</applicationFormQuestionList>';
put '<formQuestionRef>';
put '<externalIdentifier>pol</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_019);
put '</applicationFormQuestionList>';
put '<formQuestionRef>';
put '<externalIdentifier>pol</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_035);
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '</applicationFormGroupList>';
put '</applicationForm>';
put '</policyRequest>';
put '</handlePolicyApplicationRequest>';
put '</soapenv:Body>';
put '</soapenv:Envelope>';
end;
run;
And if you only want to check for the presence of a single special character in the string, use
if indexc(&DC_ID.,'&')
Your macro misses the END for the outer DO block.
If you had used proper code formatting (consistent indentation for functional blocks, see Maxim 12), this would have been obvious.
I have to correct myself, there are enough ENDs, but it needed consistent formatting for me to find this out:
%macro file_loop(DC_ID);
if &DC_ID. not in ('.',' ')
then do;
if compress(&DC_ID., '% <>&', 'k') in ('&')
then do;
put '<![CDATA[';
put '<answer>';
put '<value>' &DC_ID. +(-1) '</value>';
put '</answer>';
put ']]>';
end;
else do;
put '<answer>';
put '<value>' &DC_ID. '</value>';
put '</answer>';
end;
end;
else do;
put '<answer>';
put '<value/>';
put '</answer>';
end;
%mend file_loop;
Since this complete code:
data transformed_data;
input DC_014 $6. DC_019 $13. DC_035 $9.;
cards;
116312 tosst po&ice
118323 tosst Police Mobile
113244 tosst Police Mobile
115333 tosst Police Mobile
118333 tosst Police Mobile
;
%macro file_loop(DC_ID);
if &DC_ID. not in ('.',' ')
then do;
if compress(&DC_ID., '% <>&', 'k') in ('&')
then do;
put '<![CDATA[';
put '<answer>';
put '<value>' &DC_ID. +(-1) '</value>';
put '</answer>';
put ']]>';
end;
else do;
put '<answer>';
put '<value>' &DC_ID. '</value>';
put '</answer>';
end;
end;
else do;
put '<answer>';
put '<value/>';
put '</answer>';
end;
%mend file_loop;
data _null_;
set transformed_data;
file "/folders/myfolders/TEST_Shan1.xml";
if _n_ = 1
then do;
put '<?xml version="1.0" encoding="iso-8859-1"?>';
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>';
/*Code*/
put '<applicationForm>';
put '<applicationFormGroupList>';
put '<applicationFormGroup>';
put '<formGroupRef>';
put '<externalIdentifier>polGenStartdate</externalIdentifier>';
put '</formGroupRef>';
put '<applicationFormQuestionList>';
put '<applicationFormQuestion>';
put '<formQuestionRef>';
put '<externalIdentifier>Pol1</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_014);
put '</applicationFormQuestionList>';
put '<formQuestionRef>';
put '<externalIdentifier>pol</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_019);
put '</applicationFormQuestionList>';
put '<formQuestionRef>';
put '<externalIdentifier>pol</externalIdentifier>';
put '</formQuestionRef>';
%file_loop(DC_035);
put '</applicationFormQuestionList>';
put '</applicationFormGroup>';
put '</applicationFormGroupList>';
put '</applicationForm>';
put '</policyRequest>';
put '</handlePolicyApplicationRequest>';
put '</soapenv:Body>';
put '</soapenv:Envelope>';
end;
run;
worked without problems, what is your issue? Please describe it, if your log contains any WARNINGs, ERRORs or extraneous NOTEs, post it.
Run this:
data check;
set transformed_data;
x1 = put(compress(DC_019, '% <>&', 'k'),$hex26.);
run;
and you will see that the result of the COMPRESS function starts with a blank.
Why?
You read with formatted input, so the blanks between the columns end up as leading blanks in the strings.
If you want to get rid of blanks, compress them out of the string:
if compress(&DC_ID., '%<>&', 'k') in ('&')
By having a blank in the second argument of the COMPRESS function, you kept them.
And if you only want to check for the presence of a single special character in the string, use
if indexc(&DC_ID.,'&')
See my previous post using the INDEXC function. It does exactly what you want.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.