DATA Step, Macro, Functions and more

Facing Problem while using format in a data step

Accepted Solution Solved
Reply
Contributor
Posts: 35
Accepted Solution

Facing Problem while using format in a data step

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 ?


Accepted Solutions
Solution
‎05-17-2016 02:08 AM
Trusted Advisor
Posts: 1,118

Re: Facing Problem while using format in a data step

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.);

View solution in original post


All Replies
PROC Star
Posts: 1,760

Re: Facing Problem while using format in a data step

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)

 

 

 

Solution
‎05-17-2016 02:08 AM
Trusted Advisor
Posts: 1,118

Re: Facing Problem while using format in a data step

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.);
Contributor
Posts: 35

Re: Facing Problem while using format in a data step

Posted in reply to FreelanceReinhard

Thanks for your inputs mate. It worked.

Super User
Super User
Posts: 7,083

Re: Facing Problem while using format in a data step

[ Edited ]

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);

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 246 views
  • 1 like
  • 4 in conversation