<?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: Iteratively select column values into individual variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875177#M345799</link>
    <description>&lt;P&gt;If i should be the&amp;nbsp;&lt;EM&gt;macro variable&lt;/EM&gt; i, you need to address it as &amp;amp;i.&lt;/P&gt;</description>
    <pubDate>Thu, 11 May 2023 11:08:18 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-05-11T11:08:18Z</dc:date>
    <item>
      <title>Iteratively select column values into individual variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875175#M345797</link>
      <description>&lt;P&gt;Dear community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to iteratively select column values (nobs) into individual variables (count): nobs value of row 1 into count1, nobs value of row 2 into count2, and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used a do loop for the iteration and added a column "index" to the dataset so as to help select the specific row for each iteration, using a where clause, below is the code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro sqlloop(start=,end=); 
   proc sql; 
     %do i=&amp;amp;start. %to &amp;amp;end. %by 1; 
       select nobs
       into :count
       from table
       where index=i; 
     %end; 
   quit;
%mend; 

%sqlloop(start=1, end=5)&lt;/PRE&gt;&lt;P&gt;I got the error :&amp;nbsp;&lt;EM&gt;ERROR: Expression using equals (=) has components that are of different data types&lt;/EM&gt;, for the &lt;STRONG&gt;where index=i&lt;/STRONG&gt; clause even if the index column is numeric and i also. If I replace i by 1 in the code, there is no error but then there is of course no iteration through the rows.&lt;/P&gt;&lt;P&gt;Plus, I do not know how iteratively adapt the variable name count for each iteration (count1, count2, count3 and so on.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could you please help ?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 10:50:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875175#M345797</guid>
      <dc:creator>Hugo2</dc:creator>
      <dc:date>2023-05-11T10:50:27Z</dc:date>
    </item>
    <item>
      <title>Re: Iteratively select column values into individual variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875177#M345799</link>
      <description>&lt;P&gt;If i should be the&amp;nbsp;&lt;EM&gt;macro variable&lt;/EM&gt; i, you need to address it as &amp;amp;i.&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 11:08:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875177#M345799</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-11T11:08:18Z</dc:date>
    </item>
    <item>
      <title>Re: Iteratively select column values into individual variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875179#M345801</link>
      <description>&lt;P&gt;And your macro won't really do anything; first, only the value from the last iteration of the %DO loop will be stored in &amp;amp;count, and then the variable will vanish after the macro execution, as it will be created in the local symbol table.&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 11:11:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875179#M345801</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-05-11T11:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: Iteratively select column values into individual variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875186#M345804</link>
      <description>&lt;P&gt;The error you're getting is because the macro variable can not be accessed as dataset variable. You can access i macro variable as &amp;amp;i.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also you have mentioned variable nobs value in count1 for row1, count2 for row2,etc.&lt;/P&gt;
&lt;P&gt;I'm quite not sure what exactly you want to achieve here but here is the code that might give you some reference and you can adjust it on top of that.&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/355656"&gt;@Hugo2&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table;
	input index nobs;
	datalines;
1 10
2 90
3 32
4 63
5 100
;
run;

%macro sqlloop(start=, end=);
	proc sql;
		%do i=&amp;amp;start. %to &amp;amp;end. %by 1;
			select nobs into :count&amp;amp;i.
       			from table where index=&amp;amp;i.;
       		
       		/*print variable names along with values */
			%put print count&amp;amp;i. value: &amp;amp;&amp;amp;count&amp;amp;i.;
		%end;
	quit;

%mend;

%sqlloop(start=1, end=5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MayurJadhav_0-1683805478254.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83857i5B769240FD97A69B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MayurJadhav_0-1683805478254.png" alt="MayurJadhav_0-1683805478254.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It can also be done using data step and symput function.&lt;/P&gt;
&lt;P&gt;Here is how:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro sqlloop(start=, end=);
	data _null_;
		set table;
		call symput('count'||left(_n_), nobs);
	run;
	%do i=&amp;amp;start. %to &amp;amp;end. %by 1;
		%put print count&amp;amp;i. value: &amp;amp;&amp;amp;count&amp;amp;i.;
	%end;
%mend;
%sqlloop(start=1, end=5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="MayurJadhav_1-1683806354948.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/83858iCDF4FA1D697712C7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="MayurJadhav_1-1683806354948.png" alt="MayurJadhav_1-1683806354948.png" /&gt;&lt;/span&gt;&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>Thu, 11 May 2023 11:59:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875186#M345804</guid>
      <dc:creator>MayurJadhav</dc:creator>
      <dc:date>2023-05-11T11:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: Iteratively select column values into individual variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875268#M345837</link>
      <description>&lt;P&gt;Strong suggestion: Provide an example of the data set and what you expect the result to look like.&lt;/P&gt;
&lt;P&gt;Since you want a data set then both are best provided as data step code so we can run code against the data and compare with your desired result.&lt;/P&gt;
&lt;P&gt;The more unusual the request the more important to show start and end.&lt;/P&gt;
&lt;P&gt;Personally I cannot understand what your description means. Quite often in SAS terms NOBS is 'number of observations used or read' by a procedure. So "nobs of a value" doesn't make much sense. A count of values quite often points to Proc Freq.&lt;/P&gt;</description>
      <pubDate>Thu, 11 May 2023 15:59:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/875268#M345837</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-05-11T15:59:07Z</dc:date>
    </item>
    <item>
      <title>Re: Iteratively select column values into individual variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/876915#M346415</link>
      <description>It worked perfectly, many thanks!</description>
      <pubDate>Mon, 22 May 2023 15:23:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Iteratively-select-column-values-into-individual-variables/m-p/876915#M346415</guid>
      <dc:creator>Hugo2</dc:creator>
      <dc:date>2023-05-22T15:23:43Z</dc:date>
    </item>
  </channel>
</rss>

