Here another coding option:
data have;
dpf_base='AAA';
output;
dpf_base=' ';
output;
stop;
run;
data _null_;
file print;
set have;
move= -1-missing(dpf_base);
put ' <dynamicPricingFactor>' dpf_base +move '</dynamicPricingFactor>';
run;
Result:
<dynamicPricingFactor>AAA</dynamicPricingFactor> <dynamicPricingFactor></dynamicPricingFactor>
Still I'm not receiving the desired output. In real time, the variables (numeric) DPF_base, DPF_major_change and DPF_Minor_Change are not filled in SAS dataset then the field: <dynamicPricingFactor> in the xml file is filled in with a ‘.’ The below conversion program will do this automatically and this not the good way to fill it, it should be left empty as <dynamicPricingFactor></dynamicPricingFactor>
So please guide me where should I add your proposed solution in my code below? If I add it after the put statement I'm not receiving the desired output as the number of output lines after adding the code rose from 12327 lines to 12567.
put ' <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
My condition is actual output lines in the code should be always be 12327 even after executing the code after the changes.
data _null_;
set input.dpf_cur_mon end=fin;
by externalidentifier;
length a$200 b$200 c$300;
file "/shared/xml/dpf_cur_mon.xml";
if _n_=1 then
do;
put '<?xml version="1.0" encoding="UTF-8"?>';
end;
if first.externalidentifier then
do;
put '<dynamicPricingFactorPolicy> ';
put '<policyRef>';
put '<externalIdentifier>' externalidentifier +(-1)'</externalIdentifier>';
put '</policyRef>';
put '<dateRenewal>' dateRenewal+(-1)'</dateRenewal>';
put '<clauseCode>' clause_Base+(-1)'</clauseCode>';
put '<dynamicPricingFactorPolicyCoverList>';
end;
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>';
put '<dynamicPricingFactorPolicyCover>';
put '<productVersionCoverRef>';
put ' <externalIdentifier>' cover +(-1) '</externalIdentifier>';
put ' </productVersionCoverRef>';
put ' <isForNonMatch>true</isForNonMatch>';
put ' <hashCode1>' veh_driver_hashcode +(-1) '</hashCode1>';
put ' <hashCode2></hashCode2>';
put ' <dynamicPricingFactor>' dpf_minor_change +(-1)'</dynamicPricingFactor>';
put ' <dynamicPricingFactorPolicyCoverLangList>';
put ' <dynamicPricingFactorPolicyCoverLang>';
put ' <languageRef>';
put ' <externalIdentifier>FR</externalIdentifier>';
put ' </languageRef>';
b=translate(expl_minor_change,'-','/');
put ' <explanation>' b '</explanation>';
*put ' <explanation>' translate(expl_major_change,'-','/') '</explanation>';
put ' </dynamicPricingFactorPolicyCoverLang>';
put ' <dynamicPricingFactorPolicyCoverLang>';
put ' <languageRef>';
put ' <externalIdentifier>VLS</externalIdentifier>';
put ' </languageRef>';
*put ' <explanation>' translate(expl_major_change,'-','/') '</explanation>';
put ' <explanation>' b '</explanation>';
put ' </dynamicPricingFactorPolicyCoverLang>';
put ' </dynamicPricingFactorPolicyCoverLangList>';
put ' </dynamicPricingFactorPolicyCover>';
put '<dynamicPricingFactorPolicyCover>';
put '<productVersionCoverRef>';
put ' <externalIdentifier>' cover +(-1) '</externalIdentifier>';
put ' </productVersionCoverRef>';
put ' <isForNonMatch>true</isForNonMatch>';
put ' <hashCode1></hashCode1>';
put ' <hashCode2></hashCode2>';
put ' <dynamicPricingFactor>' dpf_major_change +(-1) '</dynamicPricingFactor>';
put ' <dynamicPricingFactorPolicyCoverLangList>';
put ' <dynamicPricingFactorPolicyCoverLang>';
put ' <languageRef>';
put ' <externalIdentifier>FR</externalIdentifier>';
put ' </languageRef>';
c=translate(expl_major_change,'-','/');
put ' <explanation>' c '</explanation>';
*put ' <explanation>' translate(expl_minor_change,'-','/') '</explanation>';
put ' </dynamicPricingFactorPolicyCoverLang>';
put ' <dynamicPricingFactorPolicyCoverLang>';
put ' <languageRef>';
put ' <externalIdentifier>VLS</externalIdentifier>';
put ' </languageRef>';
put ' <explanation>' c '</explanation>';
put ' </dynamicPricingFactorPolicyCoverLang>';
put ' </dynamicPricingFactorPolicyCoverLangList>';
put ' </dynamicPricingFactorPolicyCover>';
if last.externalidentifier then
do;
put '</dynamicPricingFactorPolicyCoverList>';
put '</dynamicPricingFactorPolicy>';
end;
if fin then
do;
put '</dynamicPricingFactorPolicyList>';
end;
run;
Sample data:
externalIdentifier | dateRenewal | Veh_Driver_hashcode | Version_subversion_hashcode | Clause_Base | Clause_Major_change | Clause_Minor_change | Cover | DPF_base | Expl_base | DPF_major_change | Expl_major_change | DPF_minor_change | Expl_minor_change |
9000345833 | 12/03/2119 | 31_834261303 | 1.0 | ClauseBase1 | ClauseMajor1 | ClauseMinor1 | covMotorbikeBA | . | FR BA x2 perextra/NL BA x2 perc extra | . | FR BA x0,5 pe xtra/NL BA x0,5 perc extra | . | FR BA x1,5 prc xtra/NL BA x1,5 perc extra |
If you want me to run your code with the data you've posted then please post this data in a friendlier form (as SAS data step creating the data).
The code I've posted should work for both character and numeric variables. Below the same approach for two numeric variables. I hope that will answer your question where and how in the code to populate and use variable move.
data have;
dpf_base=999; dpf_major_change=.; output;
dpf_base=.; dpf_major_change=123; output;
stop;
run;
data _null_;
file print;
set have;
move= -1-missing(dpf_base);
put ' <dynamicPricingFactor>' dpf_base +move '</dynamicPricingFactor>';
move= -1-missing(dpf_major_change);
put ' <dynamicPricingFactor>' dpf_major_change +move '</dynamicPricingFactor>';
run;
Result:
<dynamicPricingFactor>999</dynamicPricingFactor> <dynamicPricingFactor></dynamicPricingFactor> <dynamicPricingFactor></dynamicPricingFactor> <dynamicPricingFactor>123</dynamicPricingFactor>
I just wanted to know where I've to (in my code) add your proposed code to resolve the issue. Given below the data step which creates the data after placing the file in the location where you have access to import.
DATA INPUT.dpf_bcar_bmotorbike_201803;
LENGTH
externalIdentifier $ 11
dateRenewal $ 10
Veh_Driver_hashcode $ 19
Version_subversion_hashcode $ 3
Clause_Base $ 11
Clause_Major_change $ 12
Clause_Minor_change $ 12
Cover $ 20
DPF_base 8
Expl_base $ 65
DPF_major_change 8
Expl_major_change $ 45
DPF_minor_change 8
Expl_minor_change $ 69 ;
FORMAT
externalIdentifier $CHAR11.
dateRenewal $CHAR10.
Veh_Driver_hashcode $CHAR19.
Version_subversion_hashcode $CHAR3.
Clause_Base $CHAR11.
Clause_Major_change $CHAR12.
Clause_Minor_change $CHAR12.
Cover $CHAR20.
DPF_base F12.4
Expl_base $CHAR65.
DPF_major_change F12.4
Expl_major_change $CHAR45.
DPF_minor_change F12.4
Expl_minor_change $CHAR69. ;
INFORMAT
externalIdentifier $CHAR11.
dateRenewal $CHAR10.
Veh_Driver_hashcode $CHAR19.
Version_subversion_hashcode $CHAR3.
Clause_Base $CHAR11.
Clause_Major_change $CHAR12.
Clause_Minor_change $CHAR12.
Cover $CHAR20.
DPF_base BEST12.
Expl_base $CHAR65.
DPF_major_change BEST12.
Expl_major_change $CHAR45.
DPF_minor_change BEST12.
Expl_minor_change $CHAR69. ;
INFILE '/GTU/AGSR/wrk1/SAS_workBCE300000AD2_dlu0lsas009g.linux.dv.be/#LN00016'
LRECL=294
ENCODING="LATIN1"
TERMSTR=CRLF
DLM='7F'x
MISSOVER
DSD ;
INPUT
externalIdentifier : $CHAR11.
dateRenewal : $CHAR10.
Veh_Driver_hashcode : $CHAR19.
Version_subversion_hashcode : $CHAR3.
Clause_Base : $CHAR11.
Clause_Major_change : $CHAR12.
Clause_Minor_change : $CHAR12.
Cover : $CHAR20.
DPF_base : BEST32.
Expl_base : $CHAR65.
DPF_major_change : BEST32.
Expl_major_change : $CHAR45.
DPF_minor_change : BEST32.
Expl_minor_change : $CHAR69. ;
RUN;
The code you've posted is an import data step, whereas the code you initially posted was a list of PUT statements more likely to export data. You've switched the problem and code around here somewhere....
I didn't switched the problem anywhere in my post. If you read all my posts you see that I've only expanded the description of my problem.
Your original code with the request to remove the spaces:
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>';
put '<dynamicPricingFactorPolicyCover>';
put '<productVersionCoverRef>';
put ' <externalIdentifier>' cover +(-1) '</externalIdentifier>';
put ' </productVersionCoverRef>';
put ' <isForNonMatch>true</isForNonMatch>';
put ' <hashCode1>' veh_driver_hashcode +(-1) '</hashCode1>';
put ' <hashCode2></hashCode2>';
Your new code:
DATA INPUT.dpf_bcar_bmotorbike_201803;
LENGTH
externalIdentifier $ 11
dateRenewal $ 10
Veh_Driver_hashcode $ 19
Version_subversion_hashcode $ 3
Clause_Base $ 11
Clause_Major_change $ 12
Clause_Minor_change $ 12
Cover $ 20
DPF_base 8
Expl_base $ 65
DPF_major_change 8
Expl_major_change $ 45
DPF_minor_change 8
Expl_minor_change $ 69 ;
FORMAT
externalIdentifier $CHAR11.
dateRenewal $CHAR10.
Veh_Driver_hashcode $CHAR19.
Version_subversion_hashcode $CHAR3.
Clause_Base $CHAR11.
Clause_Major_change $CHAR12.
Clause_Minor_change $CHAR12.
Cover $CHAR20.
DPF_base F12.4
Expl_base $CHAR65.
DPF_major_change F12.4
Expl_major_change $CHAR45.
DPF_minor_change F12.4
Expl_minor_change $CHAR69. ;
INFORMAT
externalIdentifier $CHAR11.
dateRenewal $CHAR10.
Veh_Driver_hashcode $CHAR19.
Version_subversion_hashcode $CHAR3.
Clause_Base $CHAR11.
Clause_Major_change $CHAR12.
Clause_Minor_change $CHAR12.
Cover $CHAR20.
DPF_base BEST12.
Expl_base $CHAR65.
DPF_major_change BEST12.
Expl_major_change $CHAR45.
DPF_minor_change BEST12.
Expl_minor_change $CHAR69. ;
INFILE '/GTU/AGSR/wrk1/SAS_workBCE300000AD2_dlu0lsas009g.linux.dv.be/#LN00016'
LRECL=294
ENCODING="LATIN1"
TERMSTR=CRLF
DLM='7F'x
MISSOVER
DSD ;
INPUT
externalIdentifier : $CHAR11.
dateRenewal : $CHAR10.
Veh_Driver_hashcode : $CHAR19.
Version_subversion_hashcode : $CHAR3.
Clause_Base : $CHAR11.
Clause_Major_change : $CHAR12.
Clause_Minor_change : $CHAR12.
Cover : $CHAR20.
DPF_base : BEST32.
Expl_base : $CHAR65.
DPF_major_change : BEST32.
Expl_major_change : $CHAR45.
DPF_minor_change : BEST32.
Expl_minor_change : $CHAR69. ;
RUN;
The solutions posted will align with your initial code. I'm not sure what that last set of code has to do with your initial question and the solution. The solutions cannot be incorporated into the code you recently posted because it doesn't output an XML file, it imports a data set.
If this is still an extension please explain how in detail.
if dpf_base eq . then do;
put ' <dynamicPricingFactor></dynamicPricingFactor>';
end;
else do;
put ' <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
end;
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>';
put '<dynamicPricingFactorPolicyCover>';
put '<productVersionCoverRef>';
put ' <externalIdentifier>' cover +(-1) '</externalIdentifier>';
put ' </productVersionCoverRef>';
put ' <isForNonMatch>true</isForNonMatch>';
put ' <hashCode1>' veh_driver_hashcode +(-1) '</hashCode1>';
put ' <hashCode2></hashCode2>';
That's all you need. Test it.
Note that this is the solution from @joao_moreira posted yesterday, not mine, so mark their answer as correct, if it works.
I tested your code and it's not working. After executing the code, I see one extra line as highlighted below.
Excerpt from Output XML file:
<isForNonMatch>true</isForNonMatch>
<hashCode1>123456789_105136914</hashCode1>
<hashCode2></hashCode2>
<dynamicPricingFactor> </dynamicPricingFactor>
<dynamicPricingFactor></dynamicPricingFactor>
Snippet of the Code which I executed:
put ' <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
if dpf_base eq . then do;
put ' <dynamicPricingFactor></dynamicPricingFactor>';
end;
put ' <dynamicPricingFactorPolicyCoverLangList>';
put ' <dynamicPricingFactorPolicyCoverLang>';
put ' <languageRef>';
put ' <externalIdentifier>FR</externalIdentifier>';
put ' </languageRef>';
a=translate(expl_base,'-','/');
put ' <explanation>' a '</explanation>';
put ' </dynamicPricingFactorPolicyCoverLang>';
Note: I'm not getting the desired output even if I add else part in the code. Appreciate if someone of you help me to resolve this issue.
What happened to my first IF statement? Check how your code differs from mine.
@Babloo wrote:
I tested your code and it's not working. After executing the code, I see one extra line as highlighted below.
Excerpt from Output XML file:
<isForNonMatch>true</isForNonMatch>
<hashCode1>123456789_105136914</hashCode1>
<hashCode2></hashCode2>
<dynamicPricingFactor> </dynamicPricingFactor>
<dynamicPricingFactor></dynamicPricingFactor>
Snippet of the Code which I executed:
put ' <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>'; if dpf_base eq . then do; put ' <dynamicPricingFactor></dynamicPricingFactor>'; end; put ' <dynamicPricingFactorPolicyCoverLangList>'; put ' <dynamicPricingFactorPolicyCoverLang>'; put ' <languageRef>'; put ' <externalIdentifier>FR</externalIdentifier>'; put ' </languageRef>'; a=translate(expl_base,'-','/'); put ' <explanation>' a '</explanation>'; put ' </dynamicPricingFactorPolicyCoverLang>';
Note: I'm not getting the desired output even if I add else part in the code. Appreciate if someone of you help me to resolve this issue.
I'm seeing the same issue as I mentioned before even after adding the if then else part.
I'd say post your full code again, but then I'd post the exact same code I did before.
Your code, note the comments.
*Places a line with dpf_base, regardless if missing or not so this creates your extra line;
put ' <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
*If dpf_base is missing then puts the line with no space, so what you want;
if dpf_base eq . then do;
put ' <dynamicPricingFactor></dynamicPricingFactor>';
end;
My code, with comments:
*If dfp is missing post the line, with no space;
if dpf_base eq . then do;
put ' <dynamicPricingFactor></dynamicPricingFactor>';
end;
*else, if dpf_base is not missing put the value as expected;
else do;
put ' <dynamicPricingFactor>' dpf_base +(-1) '</dynamicPricingFactor>';
end;
See the difference now?
If this still doesn't work, you need to post code with data.
Didn't have time earlier but posting anyway a solution based on the code you've provided.
Thanks for - trying - to provide the sample data I've asked for. I believe you've missed to also include some data when you generated the input statement in EG 😉 I've added now some mock-up data to your input statement.
Below a fully working code sample based on the code you've posted.
DATA work.have;
INFILE datalines
DLM='|'
truncover
DSD;
INPUT
externalIdentifier : $CHAR11.
dateRenewal : $CHAR10.
Veh_Driver_hashcode : $CHAR19.
Version_subversion_hashcode : $CHAR3.
Clause_Base : $CHAR11.
Clause_Major_change : $CHAR12.
Clause_Minor_change : $CHAR12.
Cover : $CHAR20.
DPF_base : BEST32.
Expl_base : $CHAR65.
DPF_major_change : BEST32.
Expl_major_change : $CHAR45.
DPF_minor_change : BEST32.
Expl_minor_change : $CHAR69.;
datalines;
AAA|BBB|CCC|DDD|EEE|FFF|GGG|HHH|111|III|222|JJJ|333|KKK
|||||||||||||
;
RUN;
data _null_;
file print;
set have;
put '<dynamicPricingFactorPolicyCover>';
put '<productVersionCoverRef>';
move= -1-missing(cover);
put ' <externalIdentifier>' cover +move '</externalIdentifier>';
put ' </productVersionCoverRef>';
put ' <isForNonMatch>false</isForNonMatch>';
move= -1-missing(veh_driver_hashcode);
put ' <hashCode1>' veh_driver_hashcode +move '</hashCode1>';
move= -1-missing(version_subversion_hashcode);
put ' <hashCode2>' version_subversion_hashcode +move '</hashCode2>';
move= -1-missing(dpf_base);
put ' <dynamicPricingFactor>' dpf_base +move '</dynamicPricingFactor>';
put ' <dynamicPricingFactorPolicyCoverLangList>';
put ' <dynamicPricingFactorPolicyCoverLang>';
put ' <languageRef>';
put ' <externalIdentifier>FR</externalIdentifier>';
put ' </languageRef>';
a=translate(expl_base,'-','/');
move= -1-missing(a);
put ' <explanation>' a +move '</explanation>';
put ' </dynamicPricingFactorPolicyCoverLang>';
put ' <dynamicPricingFactorPolicyCoverLang>';
put ' <languageRef>';
put ' <externalIdentifier>VLS</externalIdentifier>';
put ' </languageRef>';
move= -1-missing(a);
put ' <explanation>' a +move '</explanation>';
*put ' <explanation>' translate(expl_base,'-','/') '</explanation>';
put ' </dynamicPricingFactorPolicyCoverLang>';
put ' </dynamicPricingFactorPolicyCoverLangList>';
put ' </dynamicPricingFactorPolicyCover>';
run;
Result:
<dynamicPricingFactorPolicyCover> <productVersionCoverRef> <externalIdentifier>HHH</externalIdentifier> </productVersionCoverRef> <isForNonMatch>false</isForNonMatch> <hashCode1>CCC</hashCode1> <hashCode2>DDD</hashCode2> <dynamicPricingFactor>111</dynamicPricingFactor> <dynamicPricingFactorPolicyCoverLangList> <dynamicPricingFactorPolicyCoverLang> <languageRef> <externalIdentifier>FR</externalIdentifier> </languageRef> <explanation>III</explanation> </dynamicPricingFactorPolicyCoverLang> <dynamicPricingFactorPolicyCoverLang> <languageRef> <externalIdentifier>VLS</externalIdentifier> </languageRef> <explanation>III</explanation> </dynamicPricingFactorPolicyCoverLang> </dynamicPricingFactorPolicyCoverLangList> </dynamicPricingFactorPolicyCover> <dynamicPricingFactorPolicyCover> <productVersionCoverRef> <externalIdentifier></externalIdentifier> </productVersionCoverRef> <isForNonMatch>false</isForNonMatch> <hashCode1></hashCode1> <hashCode2></hashCode2> <dynamicPricingFactor></dynamicPricingFactor> <dynamicPricingFactorPolicyCoverLangList> <dynamicPricingFactorPolicyCoverLang> <languageRef> <externalIdentifier>FR</externalIdentifier> </languageRef> <explanation></explanation> </dynamicPricingFactorPolicyCoverLang> <dynamicPricingFactorPolicyCoverLang> <languageRef> <externalIdentifier>VLS</externalIdentifier> </languageRef> <explanation></explanation> </dynamicPricingFactorPolicyCoverLang> </dynamicPricingFactorPolicyCoverLangList> </dynamicPricingFactorPolicyCover>
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: