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

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;

 

1 ACCEPTED SOLUTION
10 REPLIES 10
Kurt_Bremser
Super User

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.

Kurt_Bremser
Super User

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.

Shantaram
Calcite | Level 5
Hi KurtBremser,
In first row there is a value tosst po&ice of column DC_ID19. when we are executing
if &DC_ID. not in('.',' ') then Do;
if compress(&DC_ID.,'% <>&', 'k') ='&' then Do;/*Output of this code is & */
IF the output is & then it should display following output
put '<![CDATA[';
put '<answer>';
put '<value>' &DC_ID. +(-1) '</value>';
put '</answer>';
put ']]>';
But its not executing the particular code patch.
Kurt_Bremser
Super User

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.

Shantaram
Calcite | Level 5
i want only special character from string Ex. in tosst po&ice there is & is a special character. If their is a special character in the string then i have to execute
put '<![CDATA[';
put '<answer>';
put '<value>' &DC_ID. +(-1) '</value>';
put '</answer>';
put ']]>';
Kurt_Bremser
Super User

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.

Shantaram
Calcite | Level 5
Hi KurtBremser,

Its working.
Thank you so much.

Regards,
Shantaram.
Shantaram
Calcite | Level 5

if compress(&DC_ID., '%<>&','k') in ('&')
I want to write like this DC_019='&' then execute below part, space is not required any where. Only special character required from string EX.poli&ce output= &.

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
  • 1152 views
  • 0 likes
  • 2 in conversation