<?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: Problem with nested macro do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939749#M368955</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446510"&gt;@Eileen1496&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the code first enumerate the number of records each user_id has, then get the max_num of the records, say the user with the most number of records has 79 records, this max_num will be 79. Then there is a nest loop that go from 1 to 79, and split the data into 79 parts. Then I merge these part horizontally together.&lt;/P&gt;
&lt;P&gt;I tried the code in charge of these functions on one dataset, and it worked as I wanted. So my problem is just the format of nested loop.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ok. So that is a blow by blow description of the steps.&amp;nbsp; But what is the purpose?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main reason I ask is because it really seems like a strange approach and I cannot figure out what you are trying to do so I can suggest a more direct approach, perhaps one that does not involve splitting data and merging it back together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the issue with running the code?&amp;nbsp; Your description of the failure seemed to imply that the macro did not compile and so it did not run.&amp;nbsp; But when I copy your code and run it the macro did compile and run.&amp;nbsp; It failed of course since I do not have any of the datasets it references.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is not running for you then I suggest starting a new SAS session and submitting the macro definition and call again. Perhaps you just have left SAS in an unstable state with unbalanced quotes (or other unbalanced blocks like comments of macro definitions).&amp;nbsp; Turn on MPRINT to make sure you see the SAS code that the macro does generate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 17 Aug 2024 15:04:57 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-08-17T15:04:57Z</dc:date>
    <item>
      <title>Problem with nested macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939744#M368950</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm trying to modify one of my code to a nested loop. Below is the code I have. However, when I submit the code, sas did not start running (black text of my code, no blue output nor green warning nor red error)&lt;/P&gt;
&lt;PRE&gt;%macro repeat_code2; 
%do i = 1 %to 9;
/* Create a dataset with unique record_num values */
proc sql noprint;
    select distinct record_num
    into :record_nums separated by ' '
    from user_2&amp;amp;i._record_num;
quit;

/* Macro to split data into separate datasets based on record_num */
proc sql noprint;
    select max(record_num)
    into :max_num
    from user_2&amp;amp;i._record_num;
quit;


    %do j = 1 %to &amp;amp;max_num;
        data ework.user2&amp;amp;i._uniq_pt&amp;amp;j;
            set user_2&amp;amp;i._record_num;
            where record_num = &amp;amp;j;
        run;
		    
	data user2&amp;amp;i._uniq_pt&amp;amp;j (compress=yes);
	    set ework.user2&amp;amp;i._uniq_pt&amp;amp;j (drop = record_num);
	    run;
	%end;


/* get user_year_list */
data user_2&amp;amp;i._yr_list(compress=yes);
set ework.user_2&amp;amp;i._nqtr_info_sl(keep=user_id year);
run;

proc sort data = user_2&amp;amp;i._yr_list nodupkey;
by _all_;
run;

/* merge in information i have in wide by year and user_id */
proc sort data = user_2&amp;amp;i._yr_list;
by user_id year;
run;
proc sort data = user2&amp;amp;i._uniq_pt1;
by user_id year;
run;

/* Merge the datasets */
data clcareer.user_2&amp;amp;i._career_wide (compress=yes);
    merge user_2&amp;amp;i._yr_list (in=a)
          user2&amp;amp;i._uniq_pt1;
    by user_id year;
    if a; /* Keep only observations from user_2_yr */
run;


data clcareer.user_2&amp;amp;i._career_wide;
    set clcareer.user_2&amp;amp;i._career_wide;
    rename 
        nqtr = nqtr1
        mapped_role = mapped_role1
        rcid = rcid1
		salary = salary1;
run;

proc delete data = user_2&amp;amp;i._yr_list;
run;



%do j = 2 %to &amp;amp;max_num;

proc sort data = user2&amp;amp;i._uniq_pt&amp;amp;j;
by user_id year;
run;

/* Merge the datasets */
data clcareer.user_2&amp;amp;i._career_wide (compress=yes);
    merge clcareer.user_2&amp;amp;i._career_wide (in=a)
          user2&amp;amp;i._uniq_pt&amp;amp;j;
    by user_id year;
    if a; /* Keep only observations from user_2_yr */
run;


data clcareer.user_2&amp;amp;i._career_wide;
    set clcareer.user_2&amp;amp;i._career_wide;
    rename 
        nqtr = nqtr&amp;amp;j
        mapped_role = mapped_role&amp;amp;j
        rcid = rcid&amp;amp;j
		salary = salary&amp;amp;j;
run;
%end;
%end;
%mend;
%repeat_code2;&lt;/PRE&gt;
&lt;P&gt;At first, I thought it might be me missing end or mend or repeat_code2 somewhere, but when I try a smaller piece of code below, it works.&lt;/P&gt;
&lt;PRE&gt;%macro repeat_code; 
%do i = 1 %to 9;
    proc sql noprint;
        select max(record_num)
        into :max_num
        from user_2&amp;amp;i._record_num;
    quit;

    %do j = 1 %to &amp;amp;max_num;
        data ework.user2&amp;amp;i._uniq_pt&amp;amp;j;
            set user_2&amp;amp;i._record_num;
            where record_num = &amp;amp;j;
        run;
    %end;
%end;
%mend;
%repeat_code;
&lt;/PRE&gt;
&lt;P&gt;So could you tell me where my code is wrong?&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 13:57:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939744#M368950</guid>
      <dc:creator>Eileen1496</dc:creator>
      <dc:date>2024-08-17T13:57:38Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with nested macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939745#M368951</link>
      <description>&lt;P&gt;What is the purpose of the code?&amp;nbsp; The comments do not seem to match the code.&amp;nbsp; For example this first comment says&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create a dataset with unique record_num values */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the code after that is just making MACRO variables, not a dataset.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 14:38:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939745#M368951</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-17T14:38:01Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with nested macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939748#M368954</link>
      <description>&lt;P&gt;the code first enumerate the number of records each user_id has, then get the max_num of the records, say the user with the most number of records has 79 records, this max_num will be 79. Then there is a nest loop that go from 1 to 79, and split the data into 79 parts. Then I merge these part horizontally together.&lt;/P&gt;
&lt;P&gt;I tried the code in charge of these functions on one dataset, and it worked as I wanted. So my problem is just the format of nested loop.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 14:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939748#M368954</guid>
      <dc:creator>Eileen1496</dc:creator>
      <dc:date>2024-08-17T14:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with nested macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939749#M368955</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/446510"&gt;@Eileen1496&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;the code first enumerate the number of records each user_id has, then get the max_num of the records, say the user with the most number of records has 79 records, this max_num will be 79. Then there is a nest loop that go from 1 to 79, and split the data into 79 parts. Then I merge these part horizontally together.&lt;/P&gt;
&lt;P&gt;I tried the code in charge of these functions on one dataset, and it worked as I wanted. So my problem is just the format of nested loop.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ok. So that is a blow by blow description of the steps.&amp;nbsp; But what is the purpose?&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main reason I ask is because it really seems like a strange approach and I cannot figure out what you are trying to do so I can suggest a more direct approach, perhaps one that does not involve splitting data and merging it back together.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the issue with running the code?&amp;nbsp; Your description of the failure seemed to imply that the macro did not compile and so it did not run.&amp;nbsp; But when I copy your code and run it the macro did compile and run.&amp;nbsp; It failed of course since I do not have any of the datasets it references.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If it is not running for you then I suggest starting a new SAS session and submitting the macro definition and call again. Perhaps you just have left SAS in an unstable state with unbalanced quotes (or other unbalanced blocks like comments of macro definitions).&amp;nbsp; Turn on MPRINT to make sure you see the SAS code that the macro does generate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 15:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939749#M368955</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-17T15:04:57Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with nested macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939750#M368956</link>
      <description>&lt;P&gt;Code begins executing for me when I run it. Of course, I get the expected errors because I don't have your data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please add debugging options and run your macro and then show us the LOG (down to the first error).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen mlogic;
%repeat_code2&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's not jump to conclusions that it is the nesting that is the problem.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 15:05:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939750#M368956</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2024-08-17T15:05:56Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with nested macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939752#M368957</link>
      <description>&lt;P&gt;This code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data clcareer.user_2&amp;amp;i._career_wide;
    set clcareer.user_2&amp;amp;i._career_wide;
    rename 
        nqtr = nqtr1
        mapped_role = mapped_role1
        rcid = rcid1
		salary = salary1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;makes it look like the object is to transpose the data.&lt;/P&gt;
&lt;P&gt;If so you should be able to do that without pulling it apart and pasting it back together.&amp;nbsp; Something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.user_id);
    set have;
    by user_id year;
    array _nqtr [2000:2024] nqtr2000-nqtr2024;
    array _salary [2000:2024] salary2000-salary2024;
    array _rcid [2000:2024] rcid2000-rcid2024;
    array _mapped_role [2000:2024] mapped_role2000-mapped_role2024;
    _nqtr[year] = nqtr1;
    _salary[year]=salary1;
    _rcid[year]=rcid1;
    _mapped_role[year]=mapped_role1;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the only code modification you might want would be to replace the upper and lower bounds of the set of year values with macro variables.&lt;/P&gt;</description>
      <pubDate>Sat, 17 Aug 2024 15:48:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939752#M368957</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-17T15:48:56Z</dc:date>
    </item>
    <item>
      <title>Re: Problem with nested macro do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939871#M368983</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Hi Tom and Paige, thanks for all the comments. It took me a while to reply because I was suspecting the SAS somehow enter a freeze state since my code is only submitted but not run. So I restarted SAS and it took me a while to rerun the previous temp data sets. Then my remote computer had some problems that were fixed until now.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I add the debug option at the beginning and rerun the nested loop, turns out it works as expected! But the debug line is very helpful for me.&lt;/P&gt;</description>
      <pubDate>Mon, 19 Aug 2024 12:54:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problem-with-nested-macro-do-loop/m-p/939871#M368983</guid>
      <dc:creator>Eileen1496</dc:creator>
      <dc:date>2024-08-19T12:54:24Z</dc:date>
    </item>
  </channel>
</rss>

