<?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: Array-apply format to multiple vars in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940274#M369083</link>
    <description>&lt;P&gt;You have to define the _Bvars array as character with an appropriate length.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array _vars(*) x y z r q g;
array _Bvars(*) $20 _x _y _z _r _q _g;

do i=1 to dim(_vars);
   _Bvars(i)=put(_vars(i),Ratio_Till1_Fmt.);
end;

drop i;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 21 Aug 2024 12:24:07 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2024-08-21T12:24:07Z</dc:date>
    <item>
      <title>Array-apply format to multiple vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940273#M369082</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to use array to calculate same formula for multiple variables at once.&lt;/P&gt;
&lt;P&gt;I don't get the desired result and the calculated field&amp;nbsp;_x _y _z _r _q _g&amp;nbsp; get null values.&lt;/P&gt;
&lt;P&gt;When I calculate it manually (without array) then calculation is fine.&lt;/P&gt;
&lt;P&gt;What is wrong with my array???&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc format ;
VALUE Ratio_Till1_Fmt
-9997 ='(1) Ratio un-defined'
0='(2) 0'
0-&amp;lt;0.1='(3) (0,0.1]'
0.1-&amp;lt;0.2='(4) [0.1,0.2)'
0.2-&amp;lt;0.3='(5) [0.2,0.3)'
0.3-&amp;lt;0.4='(6) [0.3,0.4)'
0.4-&amp;lt;0.5='(7) [0.4,0.5)'
0.5-&amp;lt;0.6='(8) [0.5,0.6)'
0.6-&amp;lt;0.7='(9) [0.6,0.7)'
0.7-&amp;lt;0.8='(10) [0.7,0.8)'
0.8-&amp;lt;0.9='(11) [0.8,0.9)'
0.9-&amp;lt;1.0='(12) [0.9,1.0)'
1.0='(13) 1'
;
Run;
Data have;
input X y Z R q g;
cards;
0 0.1 0.8 0.1 1 -9997
0.2 0.3 0.4 0 0 0.4
;
Run;
data want;
set have;
array _vars(*) x y z r q g;
array _Bvars(*) _x _y _z _r _q _g;
do i=1 to dim(_vars);
_Bvars(i)=put(_vars(i),Ratio_Till1_Fmt.);
end;
drop i;
/*calc_x=put(x,Ratio_Till1_Fmt.);*/
/*calc_y=put(y,Ratio_Till1_Fmt.);*/
/*calc_z=put(z,Ratio_Till1_Fmt.);*/
/*calc_r=put(r,Ratio_Till1_Fmt.);*/
/*calc_q=put(q,Ratio_Till1_Fmt.);*/
/*calc_g=put(g,Ratio_Till1_Fmt.);*/
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Aug 2024 12:17:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940273#M369082</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-08-21T12:17:49Z</dc:date>
    </item>
    <item>
      <title>Re: Array-apply format to multiple vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940274#M369083</link>
      <description>&lt;P&gt;You have to define the _Bvars array as character with an appropriate length.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
array _vars(*) x y z r q g;
array _Bvars(*) $20 _x _y _z _r _q _g;

do i=1 to dim(_vars);
   _Bvars(i)=put(_vars(i),Ratio_Till1_Fmt.);
end;

drop i;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 21 Aug 2024 12:24:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940274#M369083</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2024-08-21T12:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Array-apply format to multiple vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940287#M369084</link>
      <description>&lt;P&gt;The clue to the problem was the notes in the log about implicit conversion from character values to numeric values:&lt;/P&gt;
&lt;PRE&gt;NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).
      30:1
NOTE: Invalid numeric data, '(2) 0' , at line 30 column 11.
NOTE: Invalid numeric data, '(4) [0.1,0.2)' , at line 30 column 11.
NOTE: Invalid numeric data, '(11) [0.8,0.9)' , at line 30 column 11.
NOTE: Invalid numeric data, '(4) [0.1,0.2)' , at line 30 column 11.
NOTE: Invalid numeric data, '(13) 1' , at line 30 column 11.
NOTE: Invalid numeric data, '(1) Ratio un-defined' , at line 30 column 11.
X=0 y=0.1 Z=0.8 R=0.1 q=1 g=-9997 _x=. _y=. _z=. _r=. _q=. _g=. i=7 _ERROR_=1 _N_=1
NOTE: Invalid numeric data, '(5) [0.2,0.3)' , at line 30 column 11.
NOTE: Invalid numeric data, '(6) [0.3,0.4)' , at line 30 column 11.
NOTE: Invalid numeric data, '(7) [0.4,0.5)' , at line 30 column 11.
NOTE: Invalid numeric data, '(2) 0' , at line 30 column 11.
NOTE: Invalid numeric data, '(2) 0' , at line 30 column 11.
NOTE: Invalid numeric data, '(7) [0.4,0.5)' , at line 30 column 11.
X=0.2 y=0.3 Z=0.4 R=0 q=0 g=0.4 _x=. _y=. _z=. _r=. _q=. _g=. i=7 _ERROR_=1 _N_=2
NOTE: There were 2 observations read from the data set WORK.HAVE.
NOTE: The data set WORK.WANT has 2 observations and 12 variables.
&lt;/PRE&gt;
&lt;P&gt;I think of those notes as errors.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SAS variables are strongly typed, in the sense that a variable is either numeric or character.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But in the DATA step language, you are not required to explicitly define the type of each variable.&amp;nbsp; If you don't define the type, the compiler will decide the type for you, based on rules for the statements/functions/etc used to create the variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you use an array statement to create new variables, and do not explicitly define the variable type, the variables are created as numeric variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then in your assignment statement, you are assigning a character value to a numeric value:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_Bvars(i)=put(_vars(i),Ratio_Till1_Fmt.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS will try to do an implicit character to numeric conversion for you,&amp;nbsp; (which causes the first NOTE which I think should be an error), and then when that conversion fails it will return a missing value (and generate the remaining notes which I think should be errors).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you don't use an array, the compiler sees:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;calc_x=put(x,Ratio_Till1_Fmt.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And says "oh, this statement is creating a new variable calc_x, it should be a character variable because Ronein is assigning a character value to it, so I'll make calc_x a character variable.&amp;nbsp; And I'll give calc_x a length of $20, because that is the longest length that can be returned by the format Ratio_Till1_Fmt."&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2024 13:00:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940287#M369084</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2024-08-21T13:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Array-apply format to multiple vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940290#M369085</link>
      <description>&lt;P&gt;Why do you need new character variables to do this? Why not just use the numeric formatted values in array&amp;nbsp; _vars?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Character variables sort alphabetically, numeric variable sort numerically. With character variables, you need the (5) to get things to sort properly; with numeric variables the results can be sorted into desired numerical order easily.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, I think it is really poor design and unprofessional to have categories&amp;nbsp;(5) [0.2,0.3) instead of [0.2,0.3). No one cares that you have to use (5) to get things to sort properly, it just confuses people viewing your results, not a good thing. I hate seeing presentations where the months in the table are (1) Jan, (2) Feb and so on. There are many other ways to get things to sort properly. If you leave the values as numeric, which is the best practice for numbers, and don't stick (5) in front of the categories in the format, many procedures have an option ORDER=INTERNAL which forces your values to sort properly; plus other methods as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2024 13:09:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940290#M369085</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-08-21T13:09:27Z</dc:date>
    </item>
    <item>
      <title>Re: Array-apply format to multiple vars</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940300#M369086</link>
      <description>Thanks.&lt;BR /&gt;I need to run logistic regression.&lt;BR /&gt;It was defined for me that worse group is 1 and then group 2 is better and so on.&lt;BR /&gt;It was defined for me that people want to see categories (groups) description that  mention if it is group 1,2,3,4,5 because it give immediate information if it is worse group or best group or in between.&lt;BR /&gt;I agree that best is to create a numeric variable that get values 1,2,3,4,5 ( group number) and add to numeric variable a format ( as I mentioned format need to add number of group too). I agree that create a char variable is not clever way</description>
      <pubDate>Wed, 21 Aug 2024 14:06:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-apply-format-to-multiple-vars/m-p/940300#M369086</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2024-08-21T14:06:02Z</dc:date>
    </item>
  </channel>
</rss>

