BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
mksredl
Fluorite | Level 6

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: 

mksredl_0-1657054514989.png

 

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!

1 ACCEPTED SOLUTION

Accepted Solutions
WarrenKuhfeld
Ammonite | Level 13

@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.

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
mksredl
Fluorite | Level 6

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.

Reeza
Super User

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?

 

 

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
WarrenKuhfeld
Ammonite | Level 13

@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.

mksredl
Fluorite | Level 6

Thank you, this is very helpful context! I appreciate the response!

mkeintz
PROC Star

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.

 

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 8 replies
  • 2404 views
  • 4 likes
  • 6 in conversation