Reputation: 735
I'm trying to concatenate retained values. This is my piece of code:
data &_output.;
set &_input.;
by cpn;
retain json_array;
if first.cpn and last.cpn then do;
flag = 'both';
concat = ('subscriptions:[{'||'"mpc" : "'||compress(mpc)||'" , '||'"contract_start_date" : "'||compress(contract_start_date)||'" '||' , "contract_end_date" : "'||compress(contract_end_date)||'"'||' , "subscription_status_code" : "'||compress(subscription_status_code)||'"'||' , '||'"promo_code" : "'||compress(promo_code)||'" '||' , "print_or_digi_flag" : "'||compress(print_or_digi_flag)||'"'||' , "payment_method_selection" : "'||compress(payment_method_selection)||'"'||', "subscription_type_code" : "'||compress(subscription_type_code)||'"'||' , "report_trial_subscription" : "'||compress(report_trial_subscription)||'"'||' , "product_desc" : "'||compress(product_desc)||'"}]');
end;
else if first.cpn then do;
flag = 'first';
concat = ('subscriptions:[{'||'"mpc" : "'||compress(mpc)||'" , '||'"contract_start_date" : "'||compress(contract_start_date)||'" '||' , "contract_end_date" : "'||compress(contract_end_date)||'"'||' , "subscription_status_code" : "'||compress(subscription_status_code)||'"'||' , '||'"promo_code" : "'||compress(promo_code)||'" '||' , "print_or_digi_flag" : "'||compress(print_or_digi_flag)||'"'||' , "payment_method_selection" : "'||compress(payment_method_selection)||'"'||', "subscription_type_code" : "'||compress(subscription_type_code)||'"'||' , "report_trial_subscription" : "'||compress(report_trial_subscription)||'"'||' , "product_desc" : "'||compress(product_desc)||'"} , ');
end;
else if last.cpn then do;
flag = 'last';
concat = ('{"mpc" : "'||compress(mpc)||'" , '||'"contract_start_date" : "'||compress(contract_start_date)||'" '||' , "contract_end_date" : "'||compress(contract_end_date)||'"'||' , "subscription_status_code" : "'||compress(subscription_status_code)||'"'||' , '||'"promo_code" : "'||compress(promo_code)||'" '||' , "print_or_digi_flag" : "'||compress(print_or_digi_flag)||'"'||' , "payment_method_selection" : "'||compress(payment_method_selection)||'"'||', "subscription_type_code" : "'||compress(subscription_type_code)||'"'||' , "report_trial_subscription" : "'||compress(report_trial_subscription)||'"'||' , "product_desc" : "'||compress(product_desc)||'" }]');
end;
else if not first.cpn and not last.cpn then do;
flag = 'none';
concat = trim(('{"mpc" : "'||compress(mpc)||'" , '||'"contract_start_date" : "'||compress(contract_start_date)||'" '||' , "contract_end_date" : "'||compress(contract_end_date)||'"'||' , "subscription_status_code" : "'||compress(subscription_status_code)||'"'||' , '||'"promo_code" : "'||compress(promo_code)||'" '||' , "print_or_digi_flag" : "'||compress(print_or_digi_flag)||'"'||' , "payment_method_selection" : "'||compress(payment_method_selection)||'"'||', "subscription_type_code" : "'||compress(subscription_type_code)||'"'||' , "report_trial_subscription" : "'||compress(report_trial_subscription)||'"'||' , "product_desc" : "'||compress(product_desc)||'"} , '));
end;
if first.cpn then json_array = trim(concat);
else json_array = trim(json_array)||trim(concat);
run;
If, for instance, there are 4 records for a cpn, and the length of json_array reaches 955 on the third record - this is where the value is trimmed and that is the final result for json_array for that cpn. Both json_array & concat are set to 10,000 positions. Why is it trimmed?
Thank you in advance.
Upvotes: 1
Views: 46
Reputation: 12465
From the SAS Documentation on TRIM() (https://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#n1io938ofitwnzn18e1hzel3u9ut.htm)
In a DATA step, if the TRIM function returns a value to a variable that has not previously been assigned a length, then that variable is given the length of the argument.
If the size of JSON_ARRAY
is not explicitly set, then it will be the length of CONCAT
. If you don't set the length of CONCAT
, then it will be the length it is first assigned (in this case, 955).
So add a FORMAT
, or LENGTH
statement and set JSON_ARRAY
(you should do CONCAT
too) and you should be good to go.
Upvotes: 3