Hello! I'm trying to apply formats to all variables in an array. Example:
proc format;
value correct
1 = 'Correct'
0,2,3 = 'Not Correct'
;
run;
data example;
input id a b c d e;
datalines;
1 0 1 1 3 2
2 0 2 1 1 1
3 1 3 2 0 1
4 0 2 2 1 0
5 3 1 0 1 3
;
run;
data example;
set example;
array varlist a--e;
do i=1 to dim(varlist);
format varlist[i] correct.;
end;
run;
When I run that code, I get the following error message:
Is there a way to apply a format to all variables in an array using similar syntax? I know there are many other solutions to the very simplified example I posted, but the actual program uses several lists of non-consecutive variables that are already stored in arrays, so they are not easily referenced by other means but are already in array format.
Thank you for the help!
@PaigeMiller and @Reeza are of course correct. Some statements in SAS are executable. Examples: IF, ELSE, SET, assignment, and so on. Others provide information but are not executable. Examples: FORMAT, LENGTH, KEEP, DROP, and so on. You cannot conditionally execute them using DATA step statements. They can be conditionally included in the program via the macro language, but that is not what you are looking for.
You don't apply a format to an array. You apply a format to the variables.
data example;
input id a b c d e;
format a--e correct.;
datalines;
1 0 1 1 3 2
2 0 2 1 1 1
3 1 3 2 0 1
4 0 2 2 1 0
5 3 1 0 1 3
;
run;
As I mentioned in my post, the actual program has several long lists of non-consecutive variables that are stored in arrays. My question is asking if there is a way to apply a format to all variables in an array, rather than having to reference them all individually.
Not really, but since you have to list the variables for the array at least once, it's just a copy/paste of that line of code isn't it?
Is there something dynamic you're trying to achieve?
@mksredl wrote:
As I mentioned in my post, the actual program has several long lists of non-consecutive variables that are stored in arrays. My question is asking if there is a way to apply a format to all variables in an array, rather than having to reference them all individually.
Same principal.
If they are non-consecutive, it doesn't matter if they are stored in an array, you have to reference each one individually.
@PaigeMiller and @Reeza are of course correct. Some statements in SAS are executable. Examples: IF, ELSE, SET, assignment, and so on. Others provide information but are not executable. Examples: FORMAT, LENGTH, KEEP, DROP, and so on. You cannot conditionally execute them using DATA step statements. They can be conditionally included in the program via the macro language, but that is not what you are looking for.
Thank you, this is very helpful context! I appreciate the response!
You can use the array to write SAS code - let's say you want to write ATTRIB statements, which can later by %INCLUDEd to generate this code
proc datasets library=work noprint;
modify have;
attrib a c e format=percent5.;
attrib b d format=10.3;
run;
quit;
Here's how:
filename tmp temp;
data _null_;
set have;
array x a c e ;
array y b d;
file tmp;
put 'attrib ' @;
do over x; vnam=vname(x); put vnam @; end;
put 'format=percent5.' ';';
put 'attrib ' @;
do over y; vnam=vname(y); put vnam @; end;
put 'format=10.3' ';';
stop;
run;
proc datasets library=work noprint;
modify have;
%include tmp / source2;
run;
quit;
So the arrays are just used as placeholders to enable variable name retrieval and insertion into an ATTRIB statement. Or if you want to do this in a DATA step, just write a FORMAT statement instead of ATTRIB. The statements are written to a temporary file which is retrieved via the %INCLUDE statement.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.