我正在尝试连接保留的值。 这是我的一段代码:
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;
例如,如果cpn有4条记录, 并且json_array的长度在第三条记录上达到955 - 这是修剪值的地方,这是该cpn的json_array的最终结果。 json_array和amp; concat设置为10,000个职位。 为什么要修剪?
提前谢谢。
答案 0 :(得分:3)
来自TRIM上的SAS文档()(https://support.sas.com/documentation/cdl/en/lefunctionsref/67960/HTML/default/viewer.htm#n1io938ofitwnzn18e1hzel3u9ut.htm)
在DATA步骤中,如果TRIM函数向先前未分配长度的变量返回值,则该变量将被赋予参数的长度。
如果未明确设置JSON_ARRAY
的大小,则其长度为CONCAT
。如果您未设置CONCAT
的长度,那么它将是首次分配的长度(在本例中为955)。
所以添加FORMAT
或LENGTH
语句并设置JSON_ARRAY
(你也应该CONCAT
),你应该好好去。