<?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: proc sql into:to create macro variable through a loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671424#M201628</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/109034"&gt;@sahoositaram555&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data pdata; length&amp;nbsp;staratum $2. time 1. Event 1.;&lt;/P&gt;
&lt;P&gt;input staratum $ time Event;&lt;/P&gt;
&lt;P&gt;0 0 0&lt;/P&gt;
&lt;P&gt;0 3 42&lt;/P&gt;
&lt;P&gt;.....&lt;/P&gt;
&lt;P&gt;1 0 4&lt;/P&gt;
&lt;P&gt;1 3 2&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;3 0 5&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 3 4&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;;run;&amp;nbsp; *************pls note .... are repetitive of the same numbers********&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select distinct(stratum)into :stratum separated by ' ' from pdata; %let stratumcount = &amp;amp;sqlObs.;&lt;BR /&gt;select min(stratum)into :min_stratum from pdata;&lt;BR /&gt;quit; *****trying to get the unique values from&amp;nbsp;stratum and the minimum value for the usage in loops********&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro maxtime();&lt;BR /&gt;%do i=&amp;amp;min_stratum %to &amp;amp;stratumcount;&lt;BR /&gt;proc sql;&lt;BR /&gt;select max(time) into: strip('macro'||%scan(&amp;amp;stratum,&amp;amp;i.)) from pdata where stratum=%scan(&amp;amp;stratum,&amp;amp;i.) and event &amp;gt;0;&lt;BR /&gt;quit;&lt;BR /&gt;%end;&lt;BR /&gt;%mend maxtime;&lt;BR /&gt;%maxtime();&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it shows a syntax error at the strip function, anyone could guide me though,please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want help with specific errors, such as shown in the log, the copy the code with the errors, warnings and notes from the Log and paste into a code box opened on the forum with the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However the "into: " instruction can only be followed by valid variable name(s), NOT an expression in an attempt to make a name, and optionally the "separated by" option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And you aren't even using the same variable names. Your data set shows "staratum" and the SQL is using stratum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Jul 2020 15:08:55 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-07-22T15:08:55Z</dc:date>
    <item>
      <title>proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671331#M201577</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data pdata; length&amp;nbsp;staratum $2. time 1. Event 1.;&lt;/P&gt;
&lt;P&gt;input staratum $ time Event;&lt;/P&gt;
&lt;P&gt;0 0 0&lt;/P&gt;
&lt;P&gt;0 3 42&lt;/P&gt;
&lt;P&gt;.....&lt;/P&gt;
&lt;P&gt;1 0 4&lt;/P&gt;
&lt;P&gt;1 3 2&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;3 0 5&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 3 4&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;;run;&amp;nbsp; *************pls note .... are repetitive of the same numbers********&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select distinct(stratum)into :stratum separated by ' ' from pdata; %let stratumcount = &amp;amp;sqlObs.;&lt;BR /&gt;select min(stratum)into :min_stratum from pdata;&lt;BR /&gt;quit; *****trying to get the unique values from&amp;nbsp;stratum and the minimum value for the usage in loops********&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro maxtime();&lt;BR /&gt;%do i=&amp;amp;min_stratum %to &amp;amp;stratumcount;&lt;BR /&gt;proc sql;&lt;BR /&gt;select max(time) into: strip('macro'||%scan(&amp;amp;stratum,&amp;amp;i.)) from pdata where stratum=%scan(&amp;amp;stratum,&amp;amp;i.) and event &amp;gt;0;&lt;BR /&gt;quit;&lt;BR /&gt;%end;&lt;BR /&gt;%mend maxtime;&lt;BR /&gt;%maxtime();&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it shows a syntax error at the strip function, anyone could guide me though,please?&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 10:59:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671331#M201577</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-22T10:59:24Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671333#M201578</link>
      <description>&lt;P&gt;Why are you still so obsessed with making your life worse by somehow using a macro loop where it is not needed?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=pdata;
class stratum;
var time;
output out=max max()=;
run;

data _null_;
set max;
call symputx(cats('macro',stratum),time,'g');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For tested code, supply example data in a data step that works by simply copy/pasting and submitting it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And &lt;STRONG&gt;ALWAYS (I MEAN THAT!)&lt;/STRONG&gt; use the "little running man" icon (right next to the one indicated in the picture) to post code:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg"&gt;&lt;img src="https://communities.sas.com/skins/images/8D8B612AA6AB1DC7E9A0812281D56E02/responsive_peak/images/image_not_found.png" alt="Bildschirmfoto 2020-04-07 um 08.32.59.jpg" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 11:10:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671333#M201578</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-22T11:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671378#M201607</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;, I genuinely appreciate your advice and if you notice this time i have crossed through those hurdles where i used to stuck in my previous topics. However, can't thank you enough for helping me when i fall in to conceptual traps starting from learning call symputx. As a beginner i personally feel that more i avoid to use loops more I'm creating holes to fall back.&lt;BR /&gt;&lt;BR /&gt;I did tryout your code, but the values don't match. Therefore requesting you to help me know if my shared code can be modified to give output. &lt;BR /&gt;%macro maxtime();&lt;BR /&gt;%do i=1 %to &amp;amp;stratumcount;&lt;BR /&gt;    proc sql;&lt;BR /&gt;    select max(time) into: %sysfunc(strip("macro"||%scan(&amp;amp;stratum,&amp;amp;i.))) from plotdata where stratum=%scan(&amp;amp;stratum,&amp;amp;i.) and event &amp;gt;0;&lt;BR /&gt;    quit;&lt;BR /&gt;%end;&lt;BR /&gt;%mend maxtime;&lt;BR /&gt;%maxtime();&lt;BR /&gt;&lt;BR /&gt;this error reported below &lt;BR /&gt;MPRINT(MAXTIME):   proc sql;&lt;BR /&gt;NOTE: Line generated by the macro function "SYSFUNC".&lt;BR /&gt;26          "macro"||3&lt;BR /&gt;            _______&lt;BR /&gt;            22&lt;BR /&gt;            76&lt;BR /&gt;MPRINT(MAXTIME):   select max(time) into: "macro"||3 from pdata where stratum=3 and event &amp;gt;0;&lt;BR /&gt;ERROR 22-322: Expecting a name.  &lt;BR /&gt;&lt;BR /&gt;ERROR 76-322: Syntax error, statement will be ignored.</description>
      <pubDate>Wed, 22 Jul 2020 13:28:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671378#M201607</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-22T13:28:37Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671392#M201613</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro maxtime();
%do i=1 %to &amp;amp;stratumcount;

proc sql;
select max(time) into: macro%scan(&amp;amp;stratum,&amp;amp;i.)  from plotdata where stratum=%scan(&amp;amp;stratum,&amp;amp;i.) and event &amp;gt;0;
quit;

%end;
%mend maxtime;
%maxtime();&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jul 2020 14:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671392#M201613</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-22T14:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671421#M201625</link>
      <description>&lt;P&gt;Post usable example data, and the expected outcome, so we can do test-driven development.&lt;/P&gt;
&lt;P&gt;AND USE THE PROPER WINDIWS FOR POSTING CODE!&lt;/P&gt;
&lt;P&gt;It's not rocket science, and it won't make your head explode. Promised.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 15:07:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671421#M201625</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-22T15:07:41Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671424#M201628</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/109034"&gt;@sahoositaram555&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data pdata; length&amp;nbsp;staratum $2. time 1. Event 1.;&lt;/P&gt;
&lt;P&gt;input staratum $ time Event;&lt;/P&gt;
&lt;P&gt;0 0 0&lt;/P&gt;
&lt;P&gt;0 3 42&lt;/P&gt;
&lt;P&gt;.....&lt;/P&gt;
&lt;P&gt;1 0 4&lt;/P&gt;
&lt;P&gt;1 3 2&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;3 0 5&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3 3 4&lt;/P&gt;
&lt;P&gt;....&lt;/P&gt;
&lt;P&gt;;run;&amp;nbsp; *************pls note .... are repetitive of the same numbers********&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select distinct(stratum)into :stratum separated by ' ' from pdata; %let stratumcount = &amp;amp;sqlObs.;&lt;BR /&gt;select min(stratum)into :min_stratum from pdata;&lt;BR /&gt;quit; *****trying to get the unique values from&amp;nbsp;stratum and the minimum value for the usage in loops********&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro maxtime();&lt;BR /&gt;%do i=&amp;amp;min_stratum %to &amp;amp;stratumcount;&lt;BR /&gt;proc sql;&lt;BR /&gt;select max(time) into: strip('macro'||%scan(&amp;amp;stratum,&amp;amp;i.)) from pdata where stratum=%scan(&amp;amp;stratum,&amp;amp;i.) and event &amp;gt;0;&lt;BR /&gt;quit;&lt;BR /&gt;%end;&lt;BR /&gt;%mend maxtime;&lt;BR /&gt;%maxtime();&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;it shows a syntax error at the strip function, anyone could guide me though,please?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want help with specific errors, such as shown in the log, the copy the code with the errors, warnings and notes from the Log and paste into a code box opened on the forum with the &amp;lt;/&amp;gt; icon.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However the "into: " instruction can only be followed by valid variable name(s), NOT an expression in an attempt to make a name, and optionally the "separated by" option.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And you aren't even using the same variable names. Your data set shows "staratum" and the SQL is using stratum.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 15:08:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671424#M201628</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-22T15:08:55Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671585#M201688</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;, thanks. certainly it resolves the error, but donno why the macro vars are not getting created even though i could check in the log that the references are perfectly being resolved.&lt;BR /&gt;Any ideas what possible reasons could be?&lt;BR /&gt;</description>
      <pubDate>Wed, 22 Jul 2020 20:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671585#M201688</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-22T20:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671596#M201691</link>
      <description>&lt;P&gt;is stratum a numeric variable or character variable? do you know for your required stratum if data exists. Posting a sample of data will be helpful. Remember the scope of macro variables in this case is within the boundaries of the macro. Any code that utilizes the macro variables must be within that boundary. Else you can use a data step and call symputx where you can control the scope of a macro variable. The following is not recommended but probably works&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%global %do i = 1 %to 6; MAC&amp;amp;i. %end; ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;prior to your macro invocation. I hard coded the value to 6 but use whatever number you need.&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jul 2020 20:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671596#M201691</guid>
      <dc:creator>smantha</dc:creator>
      <dc:date>2020-07-22T20:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671645#M201712</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/109034"&gt;@sahoositaram555&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214340"&gt;@smantha&lt;/a&gt;, thanks. certainly it resolves the error, but donno why the macro vars are not getting created even though i could check in the log that the references are perfectly being resolved.&lt;BR /&gt;Any ideas what possible reasons could be?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If your Stratum data set variable is created in a manner similar to the example data Staratum then the variable is Character and you can run into issues with comparisons such as in this line:&lt;/P&gt;
&lt;PRE&gt;MPRINT(MAXTIME): select max(time) into: "macro"||3 from pdata where stratum=3 and event &amp;gt;0;&lt;/PRE&gt;
&lt;P&gt;Proc SQL will complain because when Stratum, a character variable, is compared to 3 (NOT "3") then the types are of different types.&lt;/P&gt;
&lt;P&gt;At least one of the likely issues where you think things are resolving properly. The resolved code still has to be acceptable to the basic syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Large economy size hint: Show us the code that worked as intended without any macro variables.&lt;/P&gt;
&lt;P&gt;If you can't do that then it is time to go back and get that working first.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jul 2020 00:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671645#M201712</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-23T00:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: proc sql into:to create macro variable through a loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671666#M201727</link>
      <description>Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;,&lt;BR /&gt;You have guess it right. mistakenly i had written Staratum instead of Stratum,means Stratum is a character one. I've resolved that issue by modifying the code posted below. Pls check.&lt;BR /&gt;&lt;BR /&gt;option mprint mlogic symbolgen;&lt;BR /&gt;%macro maxtime();&lt;BR /&gt;%do i=1 %to &amp;amp;stratumcount;&lt;BR /&gt;    proc sql;&lt;BR /&gt;    select max(time) into: macro%scan(&amp;amp;stratum,&amp;amp;i.) from plotdata where event &amp;gt;0 and stratum="%scan(&amp;amp;stratum,&amp;amp;i.)"  ;&lt;BR /&gt;    quit;&lt;BR /&gt;%end;&lt;BR /&gt;%mend maxtime;&lt;BR /&gt;%maxtime();  &lt;BR /&gt;</description>
      <pubDate>Thu, 23 Jul 2020 04:53:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/proc-sql-into-to-create-macro-variable-through-a-loop/m-p/671666#M201727</guid>
      <dc:creator>sahoositaram555</dc:creator>
      <dc:date>2020-07-23T04:53:38Z</dc:date>
    </item>
  </channel>
</rss>

