proc format;
value $err
'name' = '0001'
'address' = '0002'
;
run;
proc format;
value $lgth
'name' = 40
'address' = 48
;
run;
%macro check;
%let rcnt = %sysfunc(countw(&reqd_flds.,':'));
/*where reqd_flds are extracted using proc sql from a table */
%do x = 1 %to &rcnt.;
%let reqdFld&x. = %scan(&reqd_flds., %eval(&x+0), ':');
%end;
data input_data_2_output error_file_output;
set input_data_orig;
%do i = 1 %to %eval(&rcnt. - 1);
inlgth = input(put(&&reqdFld&i.,$lgth.),3.);
if length(&&reqdFld&i) > inlgth then do;
err_cd = put(&&reqdFld&i.,$err.);
err_cnt = 1;
rec_num = _n_;
record = 'BAD RECORD';
output error_file_output;
end;
%end;
%mend check_records;
%check_records;
I am not getting inlgth & err_cd getting resolved correctly. Can someone please guide on what am i doing wrong here ?
Hi @GunnerEP,
The issue is: Macro variable REQD_FLDS presumably contains a list of names of character variables, including name and address. When you check the lengths of the variable values (e.g. 'Miller' [length 6] and '123 Main St' [length 11]) with length(&&reqdFld&i), this is fine. But when you want to apply your formats to the variable names, you must put the macro variable reference &&reqdFld&i between double quotes. Otherwise, you apply the formats to the variable values, which makes no sense.
Therefore, please try:
inlgth = input(put("&&reqdFld&i.",$lgth.),3.);
and
err_cd = put("&&reqdFld&i.",$err.);
Dificult to know with out test data. The code looks liek it should work.
This should work better as it's simpler:
proc format;
value $err
'name' = '0001'
'address' = '0002'
;
value $lgth
'name' = '40'
'address' = '48'
;
run;
%macro check;
%let rcnt = %sysfunc(countw(&reqd_flds.,':'));
/*where reqd_flds are extracted using proc sql from a table */
data input_data_2_output error_file_output;
set input_data_orig;
%do i = 1 %to %eval(&rcnt. - 1);
%let reqdFld = %scan(&reqd_flds., &i , ':');
inlgth = input(put(&reqdFld.,$lgth.),3.);
if length(&reqdFld.) > inlgth then do;
err_cd = put(&reqdFld.,$err.);
err_cnt = 1;
rec_num = _n_;
record = 'BAD RECORD';
output error_file_output;
end;
%end;
%mend check_records;
%check_records;
(untested)
Hi @GunnerEP,
The issue is: Macro variable REQD_FLDS presumably contains a list of names of character variables, including name and address. When you check the lengths of the variable values (e.g. 'Miller' [length 6] and '123 Main St' [length 11]) with length(&&reqdFld&i), this is fine. But when you want to apply your formats to the variable names, you must put the macro variable reference &&reqdFld&i between double quotes. Otherwise, you apply the formats to the variable values, which makes no sense.
Therefore, please try:
inlgth = input(put("&&reqdFld&i.",$lgth.),3.);
and
err_cd = put("&&reqdFld&i.",$err.);
Thanks for your inputs mate. It worked.
Do you want to apply the format to the value of the variable named by the macro variable? Or to the NAME of the variable, that is the value of the macro variable. Also there is no need to constantly call the PUT() function for every observation. Just call it when the macro is running and generate the value as a literal for the DATA step to use.
%macro check(varlist);
%local x field;
data input_data_2_output error_file_output;
set input_data_orig;
%do x = 1 %to %sysfunc(countw(&varlist,:));
%let field = %scan(&varlist, &x, :);
if length(&field) > %sysfunc(putc(&field,$lgth)) then do;
err_cd = "%sysfunc(putc(&field,$err))";
err_cnt = 1;
rec_num = _n_;
record = 'BAD RECORD';
output error_file_output;
end;
%end;
run;
%mend check_records;
%let &reqd_flds=name:address ;
%check_records(&reqd_flds);
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.