<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do you apply a format to all variables in an array? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821715#M324411</link>
    <description>&lt;P&gt;You don't apply a format to an array. You apply a format to the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 05 Jul 2022 21:01:21 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2022-07-05T21:01:21Z</dc:date>
    <item>
      <title>How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821714#M324410</link>
      <description>&lt;P&gt;Hello! I'm trying to apply formats to all variables in an array. Example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I run that code, I get the following error message:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mksredl_0-1657054514989.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/72966i37805D439D8FDC59/image-size/medium?v=v2&amp;amp;px=400" role="button" title="mksredl_0-1657054514989.png" alt="mksredl_0-1657054514989.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the help!&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 20:57:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821714#M324410</guid>
      <dc:creator>mksredl</dc:creator>
      <dc:date>2022-07-05T20:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821715#M324411</link>
      <description>&lt;P&gt;You don't apply a format to an array. You apply a format to the variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jul 2022 21:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821715#M324411</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-05T21:01:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821716#M324412</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 21:04:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821716#M324412</guid>
      <dc:creator>mksredl</dc:creator>
      <dc:date>2022-07-05T21:04:43Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821721#M324413</link>
      <description>&lt;P&gt;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?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there something dynamic you're trying to achieve?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 21:19:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821721#M324413</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-07-05T21:19:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821726#M324418</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/426700"&gt;@mksredl&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Same principal.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If they are non-consecutive, it doesn't matter if they are stored in an array, you have to reference each one individually.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jul 2022 21:33:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821726#M324418</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-05T21:33:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821757#M324436</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;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.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jul 2022 01:42:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821757#M324436</guid>
      <dc:creator>WarrenKuhfeld</dc:creator>
      <dc:date>2022-07-06T01:42:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821766#M324445</link>
      <description>It is possible to apply format to a variable list. Here is an example:&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Assigning-SAS-formats-to-a-list-of-variables/m-p/114336" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Assigning-SAS-formats-to-a-list-of-variables/m-p/114336&lt;/A&gt;</description>
      <pubDate>Wed, 06 Jul 2022 02:59:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821766#M324445</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2022-07-06T02:59:50Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821772#M324450</link>
      <description>&lt;P&gt;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&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc datasets library=work noprint;
  modify have;
    attrib a c e format=percent5.;
    attrib b d format=10.3;
  run;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's how:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the arrays are just used as placeholders to enable variable name retrieval and insertion into an ATTRIB statement.&amp;nbsp; Or if you want to do this in a DATA step, just write a FORMAT statement instead of ATTRIB.&amp;nbsp; &amp;nbsp;The statements are written to a temporary file which is retrieved via the %INCLUDE statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Jul 2022 04:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/821772#M324450</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-07-06T04:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: How do you apply a format to all variables in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/823503#M325157</link>
      <description>&lt;P&gt;Thank you, this is very helpful context! I appreciate the response!&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 13:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-apply-a-format-to-all-variables-in-an-array/m-p/823503#M325157</guid>
      <dc:creator>mksredl</dc:creator>
      <dc:date>2022-07-15T13:37:43Z</dc:date>
    </item>
  </channel>
</rss>

