Hello
I am using array to divide all numeric variables by 100 and then I want to apply format percent9.2 to all of numeric variables.
I receive an error
Data a;
input x1 x2 x3;
cards;
2.14 2.46 3.3
19.83 18.57 18.06
35.43 33.39 36.44
3.66 2.73 4.68
;
run;
data b;
set a;
array Nums[*] _numeric_;
do i=1 to dim(Nums);
Nums(i)=Nums(i)/100;
format Nums(i) percent9.2;
end;
drop i;
Run;
/*8514 format Nums(i) percent9.2;*/
/* -*/
/* 85*/
/* 76*/
/*ERROR 85-322: Expecting a format name.*/
/**/
/*ERROR 76-322: Syntax error, statement will be ignored.*/
format Nums(i) percent9.2;
is syntactically incorrect. Should be:
format _numeric_ percent9.2 ;
Kind regards
Paul D.
format Nums(i) percent9.2;
is syntactically incorrect. Should be:
format _numeric_ percent9.2 ;
Kind regards
Paul D.
Thank you very much.
May you please explain why "format Nums(i) percent9.2;" is incorrect?
The FORMAT statement is a compile-time directive applied to the non-automatic numeric PDV variables. Nums[i] is a run-time directive. _NUMERIC_ is a compile-time list directive applied to the numeric variables the compiler has seen before the directive is issued. Thus, the numeric variables the compiler sees after the statement:
format _numeric_ <format-specs> ;
will not be affected by the format. Hence, if you want all the numeric variables in the DATA step to be affected by the statement, code it after the compiler sees its last numeric variable.
Alternatively, simply list all numeric variables in the statement instead of _NUMERIC_, in which case the placement of the FORMAT statement doesn't matter since the compiler postpones applying it after the compilation is done. The automatic numeric variables, such as _N_, _ERROR_, _IORC_ aren't affected in either case, so that if you code:
put _numeric_ ;
or
put _all_ ;
you won't see them formatted in the log using <format-specs>. However, you can print them formatted by applying the format of your choice to them in the PUT statement, such as:
put _n_ z5. ;
Kind regards
Paul D.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.