BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GunnerEP
Obsidian | Level 7

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 ?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

4 REPLIES 4
ChrisNZ
Tourmaline | Level 20

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)

 

 

 

FreelanceReinh
Jade | Level 19

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.);
GunnerEP
Obsidian | Level 7

Thanks for your inputs mate. It worked.

Tom
Super User Tom
Super User

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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