<?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 Loop, read and write in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611298#M178148</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I admit that this question could be already answered before. At least, in separate parts, but.....&lt;/P&gt;&lt;P&gt;I'm a newbie &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; and can't join it together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to do a proc corr through all variables from a data set (HAVE). I need to do it in a table with the results in pairs (if i use proc corr all variables it will create a triangular matrix&amp;nbsp; with repetitions - VAR1 with VAR 2 and Var 2 with var 1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I want to make it with this steps:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1- A loop that gets the VAR1 name, and the VAR 2 and make a proc corr with this two variables and write it to a new table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR2 (name); result&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2- Next cicle: VAR1 and VAR 3, and so on:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR2 (name); result&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR3 (name); result&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;VAR1 (name); VARN (name); result,&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; where N=count variables of the dataset (HAVE)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3- When this is complete, it should go to the next VAR and write:&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR2 (name); result&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR3 (name); result&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;VAR1 (name); VARN (name); result&lt;/P&gt;&lt;P&gt;+&lt;/P&gt;&lt;P&gt;VAR2 (name); VAR3 (name); result&lt;/P&gt;&lt;P&gt;VAR2 (name); VAR4 (name); result&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;VAR2 (name); VARN (name); result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This means, that there is a loop that takes a VAR i&amp;nbsp; and a second loop witch begins in VAR i+1 (because i don´t need to repeat VAR2 with VAR 1 again)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Resuming,&lt;/P&gt;&lt;P&gt;1-get the variables names with a loop&lt;/P&gt;&lt;P&gt;2-get variables names inside previous loop&lt;/P&gt;&lt;P&gt;3-execute proc corr&lt;/P&gt;&lt;P&gt;4-Write the results (VAR names and r value) in a separate table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The proc corr, should be something like this:&lt;/P&gt;&lt;PRE class="sascode"&gt;proc corr data=HAVE;
   var VAR1 VAR2;
 run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;THANK YOU IN ADVANCE FOR YOUR PRECIOUS THINKING TIME!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Krauss&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 12 Dec 2019 13:52:38 GMT</pubDate>
    <dc:creator>Krauss</dc:creator>
    <dc:date>2019-12-12T13:52:38Z</dc:date>
    <item>
      <title>Loop, read and write</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611298#M178148</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I admit that this question could be already answered before. At least, in separate parts, but.....&lt;/P&gt;&lt;P&gt;I'm a newbie &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; and can't join it together.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to do a proc corr through all variables from a data set (HAVE). I need to do it in a table with the results in pairs (if i use proc corr all variables it will create a triangular matrix&amp;nbsp; with repetitions - VAR1 with VAR 2 and Var 2 with var 1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I want to make it with this steps:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1- A loop that gets the VAR1 name, and the VAR 2 and make a proc corr with this two variables and write it to a new table:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR2 (name); result&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2- Next cicle: VAR1 and VAR 3, and so on:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR2 (name); result&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR3 (name); result&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;VAR1 (name); VARN (name); result,&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; where N=count variables of the dataset (HAVE)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3- When this is complete, it should go to the next VAR and write:&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR2 (name); result&lt;/P&gt;&lt;P&gt;VAR1 (name); VAR3 (name); result&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;VAR1 (name); VARN (name); result&lt;/P&gt;&lt;P&gt;+&lt;/P&gt;&lt;P&gt;VAR2 (name); VAR3 (name); result&lt;/P&gt;&lt;P&gt;VAR2 (name); VAR4 (name); result&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;VAR2 (name); VARN (name); result.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This means, that there is a loop that takes a VAR i&amp;nbsp; and a second loop witch begins in VAR i+1 (because i don´t need to repeat VAR2 with VAR 1 again)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Resuming,&lt;/P&gt;&lt;P&gt;1-get the variables names with a loop&lt;/P&gt;&lt;P&gt;2-get variables names inside previous loop&lt;/P&gt;&lt;P&gt;3-execute proc corr&lt;/P&gt;&lt;P&gt;4-Write the results (VAR names and r value) in a separate table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The proc corr, should be something like this:&lt;/P&gt;&lt;PRE class="sascode"&gt;proc corr data=HAVE;
   var VAR1 VAR2;
 run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;THANK YOU IN ADVANCE FOR YOUR PRECIOUS THINKING TIME!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Krauss&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 13:52:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611298#M178148</guid>
      <dc:creator>Krauss</dc:creator>
      <dc:date>2019-12-12T13:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, read and write</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611309#M178157</link>
      <description>&lt;P&gt;If I am understanding you properly, you can work with the correlation matrix, and just loop through it to get the results you are asking for. There is no need to include PROC CORR in the loop; in fact including PROC CORR in the loop would be extremely inefficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc corr nosimple data=sashelp.cars outp=corrs noprint;
    var _numeric_;
run;

data _null_;
    length string $ 128;
    set corrs(where=(_type_='CORR'));
	array x _numeric_;
	do i=1 to dim(x);
	    string = cat("Correlation Between ",trim(_name_)," and ",vname(x(i)),'= ',x(i));
		put string;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you don't want the correlations on the diagonal, don't write them out. If you only want the lower or upper part of the correlation matrix, you can eliminate the parts you don't want. Naturally, if you don't want results in this format, its easy to produce any other format you'd like, such as a SAS data set with each correlation in its own row.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 14:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611309#M178157</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-12T14:49:37Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, read and write</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611311#M178158</link>
      <description>&lt;P&gt;Here's an approach using a data _null_ step:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.class(obs=1) indsname=dsname;
array num_vars(3) Age Height Weight;
do i=2 to dim(num_vars);
	call execute(cat(
		'proc corr data=',dsname,' out=','output_',vname(num_vars(1)),'_',vname(num_vars(i)),'; ',
			'var ',vname(num_vars(1)),' ',vname(num_vars(i)),';',
		'run;'
		)
	);
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;-unison&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 14:15:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611311#M178158</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2019-12-12T14:15:24Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, read and write</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611314#M178160</link>
      <description>&lt;P&gt;So, you basically want to execute a PROC CORR for all the variables (or a well defined subset of variables) in a table?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest thing is probably to use SQL to generate the code, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql noprint;
  select catx(' ','PROC CORR data=SASHELP.CLASS outs=',catx('_','corr',a.name,b.name),'noprint;var',a.name,b.name,';run;') into :doit separated by ';' 
  from dictionary.columns a join dictionary.columns b on
  a.libname=b.libname and a.memname=b.memname and a.name&amp;lt;b.name
  where a.libname='SASHELP' and a.memname='CLASS' and a.type='num' and b.type='num';
quit;

&amp;amp;doit&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Dec 2019 14:28:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611314#M178160</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-12-12T14:28:44Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, read and write</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611334#M178171</link>
      <description>&lt;P&gt;Why would you want to run PROC CORR multiple times when you can run it once and get all of the correlations at once?&lt;/P&gt;
&lt;P&gt;You can just reformat the output if you want.&amp;nbsp; Here is a method using PROC TRANSPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc corr data=sashelp.class noprint outp=step1 ;
  var _numeric_;
run;

proc print data=step1;
run;

proc transpose data=step1 name=var2 
  out=want(keep=var2 _name_ col1 
            rename=(col1=corr _name_=var1)
            where=(var1 &amp;lt; var2)
           ) 
;
  where _type_='CORR';
  by _name_ notsorted ;
  var _numeric_ ;
run;

proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;The SAS System

Obs    _TYPE_    _NAME_      Age       Height     Weight

 1      MEAN               13.3158    62.3368    100.026
 2      STD                 1.4927     5.1271     22.774
 3      N                  19.0000    19.0000     19.000
 4      CORR     Age        1.0000     0.8114      0.741
 5      CORR     Height     0.8114     1.0000      0.878
 6      CORR     Weight     0.7409     0.8778      1.000

The SAS System

Obs    var1       var2       corr

 1     Age       Height    0.81143
 2     Age       Weight    0.74089
 3     Height    Weight    0.87779

&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Dec 2019 15:04:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611334#M178171</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-12T15:04:40Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, read and write</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611414#M178183</link>
      <description>&lt;P&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thank you. I tried your code and it writes only one "string" for each line. It means that it doens't append to a different table all the correlations that were calculated (On the other hand it doensn't contains the second loop)&lt;BR /&gt;I apologise if there is something I should do/understand (I'm newbie)&lt;/P&gt;&lt;P&gt;@ unison&lt;/P&gt;&lt;P&gt;Thank you. The problem with your solution is: If my table has 500 features?&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thank you. It's perfect!&lt;/P&gt;&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&lt;/P&gt;&lt;P&gt;Thank you. It's perfect!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;THANK YOU!&lt;/P&gt;&lt;P&gt;I humbly thank you for the answers and how quickly you gave them.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 18:22:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611414#M178183</guid>
      <dc:creator>Krauss</dc:creator>
      <dc:date>2019-12-12T18:22:36Z</dc:date>
    </item>
    <item>
      <title>Re: Loop, read and write</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611423#M178188</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/303353"&gt;@Krauss&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;BR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Thank you. I tried your code and it writes only one "string" for each line. It means that it doens't append to a different table all the correlations that were calculated (On the other hand it doensn't contains the second loop)&lt;BR /&gt;I apologise if there is something I should do/understand (I'm newbie)&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I think it does everything you asked for. It only needs one loop, all the correlations are handled in one loop.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 19:05:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Loop-read-and-write/m-p/611423#M178188</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-12T19:05:52Z</dc:date>
    </item>
  </channel>
</rss>

