<?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: How to create multiple variables with date suffix in a do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859839#M339680</link>
    <description>&lt;P&gt;I made a typo in the code:&lt;/P&gt;&lt;P&gt;Following is the corrected version:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro dis_status(startdate,enddate);
%let sasstart = %sysfunc(inputn(%substr(&amp;amp;startdate,3,6),yymmdd6.));
%put &amp;amp;sasstart;
%let sasend = %sysfunc(inputn(%substr(&amp;amp;enddate,3,6),yymmdd6.));
%put &amp;amp;sasend;

%do sasdate=&amp;amp;sasstart %to &amp;amp;sasend;
	%let date= %sysfunc(putn(&amp;amp;sasdate,yymmdd6.));
	%put &amp;amp;date;

     data master2;
       set master1;
		if code22_&amp;amp;date. in ('1','2','3','4') 
				then status22_&amp;amp;date. = "G "; 
		else if CODE22_&amp;amp;date. in ('5','6') 
		                                 then status22_&amp;amp;date. = "IE"; 
		else if CODE22_&amp;amp;date. in ('7','8') 
				then status22_&amp;amp;date. = "NR"; 
		else CODE22 in ('10,'11')
				then status22_&amp;amp;date. = "UE"; 
       run;
%end;
%mend;

%dis_status(20220608,20221019);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Feb 2023 05:26:36 GMT</pubDate>
    <dc:creator>lydiawawa</dc:creator>
    <dc:date>2023-02-21T05:26:36Z</dc:date>
    <item>
      <title>How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859830#M339679</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to create multiple variables based on conditions with date as their suffix. The following code only generate one variable from the end date. How do I make it generate multiple&amp;nbsp;variables based on dates in between the end and start date, and keep them on master2?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro dis_status(startdate,enddate);
%let sasstart = %sysfunc(inputn(%substr(&amp;amp;startdate,3,6),yymmdd6.));
%put &amp;amp;sasstart;
%let sasend = %sysfunc(inputn(%substr(&amp;amp;enddate,3,6),yymmdd6.));
%put &amp;amp;sasend;

%do sasdate=&amp;amp;sasstart %to &amp;amp;sasend;
	%let date= %sysfunc(putn(&amp;amp;sasdate,yymmdd6.));
	%put &amp;amp;date;

     data master2;
       set master1;
		if code22_&amp;amp;date. in ('1','2','3','4') 
				then status22_&amp;amp;date. = "G "; 
		else if CODE22_&amp;amp;date. in ('5','6') 
		                                 then status22_&amp;amp;date. = "IE"; 
		else if CODE22_&amp;amp;date. in ('7','8') 
				then status_&amp;amp;date. = "NR"; 
		else CODE22 in ('10,'11')
				then status_&amp;amp;date. = "UE"; 
       run;
%end;
%mend;

%dis_status(20220608,20221019);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-macro-do/m-p/859820#" target="_blank" rel="noopener"&gt;Add Tags&lt;/A&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 21 Feb 2023 03:35:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859830#M339679</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2023-02-21T03:35:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859839#M339680</link>
      <description>&lt;P&gt;I made a typo in the code:&lt;/P&gt;&lt;P&gt;Following is the corrected version:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;%macro dis_status(startdate,enddate);
%let sasstart = %sysfunc(inputn(%substr(&amp;amp;startdate,3,6),yymmdd6.));
%put &amp;amp;sasstart;
%let sasend = %sysfunc(inputn(%substr(&amp;amp;enddate,3,6),yymmdd6.));
%put &amp;amp;sasend;

%do sasdate=&amp;amp;sasstart %to &amp;amp;sasend;
	%let date= %sysfunc(putn(&amp;amp;sasdate,yymmdd6.));
	%put &amp;amp;date;

     data master2;
       set master1;
		if code22_&amp;amp;date. in ('1','2','3','4') 
				then status22_&amp;amp;date. = "G "; 
		else if CODE22_&amp;amp;date. in ('5','6') 
		                                 then status22_&amp;amp;date. = "IE"; 
		else if CODE22_&amp;amp;date. in ('7','8') 
				then status22_&amp;amp;date. = "NR"; 
		else CODE22 in ('10,'11')
				then status22_&amp;amp;date. = "UE"; 
       run;
%end;
%mend;

%dis_status(20220608,20221019);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Feb 2023 05:26:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859839#M339680</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2023-02-21T05:26:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859853#M339685</link>
      <description>&lt;P&gt;First, you are complicating things using that %substr in the input. Just convert the whole value.&lt;/P&gt;
&lt;PRE&gt;%let sasstart = %sysfunc(inputn(&amp;amp;startdate,yymmdd8.));
%put &amp;amp;sasstart;
%let sasend = %sysfunc(inputn(&amp;amp;enddate,yymmdd8.));
%put &amp;amp;sasend;&lt;/PRE&gt;
&lt;P&gt;You aren't very clear about what you want. But I think the straight translation of what I think you meant to attempt is to place the MACRO DO loop inside the data step. Otherwise you were overwriting the same data set multiple times and only getting the value of the enddate as a result.&lt;/P&gt;
&lt;PRE&gt;data master2;
   set master1;

   %do sasdate=&amp;amp;sasstart %to &amp;amp;sasend;
	   %let date= %sysfunc(putn(&amp;amp;sasdate,yymmdd6.));

      if code22_&amp;amp;date. in ('1','2','3','4') 
      then status22_&amp;amp;date. = "G "; 
      else if CODE22_&amp;amp;date. in ('5','6') 
      then status22_&amp;amp;date. = "IE"; 
      else if CODE22_&amp;amp;date. in ('7','8') 
      then status_&amp;amp;date. = "NR"; 
      else CODE22 in ('10','11')
      then status_&amp;amp;date. = "UE"; 
   %end;
   run;&lt;/PRE&gt;
&lt;P&gt;Addition: Is this line&lt;/P&gt;
&lt;PRE&gt; else CODE22 in ('10','11')&lt;/PRE&gt;
&lt;P&gt;supposed to be as in the first 3 comparisons.&lt;/P&gt;
&lt;PRE&gt; else CODE22_&amp;amp;date. in ('10','11')&lt;/PRE&gt;
&lt;P&gt;Do note that you were missing a ' around the&amp;nbsp; 10 in both versions of the code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general placing data into the variable names, such as these data&amp;nbsp; suffixes is often an indication of poor data structure for programming in SAS and tends to reflect spreadsheet thinking. For most analysis or reporting having each date on a separate observation with a date named and valued variable to makes the group leads to much simpler coding, analysis and reporting.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 07:29:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859853#M339685</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-21T07:29:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859857#M339689</link>
      <description>&lt;P&gt;Why to use macro loop at all? Array seems to be better solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data master1;
length code22_1-code22_5 status22_1-status22_5 $ 2;
code22_1   = "1";
code22_2   = "5";
code22_3   = "7";
code22_4   = "10";
code22_5   = "12";
run;
title "Before";
proc print;
run;




data master2;
  set master1;

  array codeVariable code22_:;
  array statusVariable status22_:;
   
  DO OVER codeVariable;
    if codeVariable in ('1','2','3','4') 
    then statusVariable = "G "; 
    else if codeVariable in ('5','6') 
    then statusVariable = "IE"; 
    else if codeVariable in ('7','8') 
    then statusVariable = "NR"; 
    else if codeVariable in ('10','11')
    then statusVariable = "UE";
    else statusVariable = "XX"; /* for unknown cases */
  end;
run;
title "After";
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 07:48:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859857#M339689</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2023-02-21T07:48:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859858#M339690</link>
      <description>&lt;P&gt;Your dataset layout causes your problems. Do not keep data (dates) in structure (variable names).&lt;/P&gt;
&lt;P&gt;Avoid transposing to a wide layout while you process data, do this only at the end when creating reports for human consumption or when certain procedures (regression) need it.&lt;/P&gt;
&lt;P&gt;If you receive data in a wide layout, transpose to long as a first step. For help with this, post an example of your master1 dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead of the IF/THEN/ELSE IF chain create a format and use a single PUT function for the conversion. Adding a line to the PROC FORMAT code is less work than adding another IF statement.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 08:00:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859858#M339690</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-02-21T08:00:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859918#M339710</link>
      <description>I need to have the suffix to be named after the dates within the range entered. Those variables will be passed to another macro and be called with the names: status&amp;amp;date.</description>
      <pubDate>Tue, 21 Feb 2023 14:05:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859918#M339710</guid>
      <dc:creator>lydiawawa</dc:creator>
      <dc:date>2023-02-21T14:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859963#M339736</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30435"&gt;@lydiawawa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;I need to have the suffix to be named after the dates within the range entered. Those variables will be passed to another macro and be called with the names: status&amp;amp;date.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Agreeing with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;— your data is poorly structured and this is causing you problems. Calendar dates (and any data) do not&amp;nbsp; belong in variable names. It is usually much easier to work with data in a different structure. Please take a step back, and explain the big picture of this project — what the goals are and what output analyses/reports you need, and not that you need certain pieces of code to do something — and then we ought to be able to provide a better data structure which results in easier programming.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 17:20:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859963#M339736</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-02-21T17:20:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to create multiple variables with date suffix in a do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859971#M339740</link>
      <description>&lt;P&gt;This is probably closer to what you're attempting to do, making some assumptions:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Some things to consider:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;You were using yymmdd6 but your dates appear to have 8 digits? Not sure your dates were being correctly derived.&lt;/LI&gt;
&lt;LI&gt;At one point you reference code22 not code22_&amp;amp;date? Is that a typo?&lt;/LI&gt;
&lt;LI&gt;data master2; set master1. Every time you loop you replace it with the original data set, and any previous changes are lost so only the last results would be retained.&lt;/LI&gt;
&lt;LI&gt;&lt;STRIKE&gt;Assumes your variables are code22_YYYYMMDD and status22_YYYYMMDD. If they have a different structure you need to modify how the variable lists are being created.&amp;nbsp;&lt;/STRIKE&gt;&lt;/LI&gt;
&lt;LI&gt;EDIT: Changed as I assume your variables are actually codeYYMMDD and statusYYMMDD not code22_ and status_ ...will leave further changes up to you.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;suggested, arrays are a better option here. If you have require the date in the name for whatever reason, then dynamically generate it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Code is untested as there is no sample data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro dis_status(startdate,enddate);

data date_list;
do date = input("&amp;amp;startdate", yymmdd8.) to input("&amp;amp;enddate", yymmdd8.);
code_list = catt('code', put(date, yymmddn6.));
status_list = catt('status', put(date, yymmddn6.));
output;
end;
run;

*create list of variables for array;
proc sql noprint;
select code_list into :code_list separated by " " from date_list;
select status_list into :status_list separated by " " from date_list;
quit;



     data master2;
       set master1;
       
        array _code(*) &amp;amp;code_list.;
        array _status(*) &amp;amp;status_list.;
		do i=1 to dim(_code);
		if _code(i) in ('1','2','3','4') 
				then _status(i) = "G "; 
		else if _code(i) in ('5','6') 
		                                 then _status(i) = "IE"; 
		else if _code(i) in ('7','8') 
				then _status(i) = "NR"; 
		else _code(i) in ('10', '11')
				then _status(i) = "UE"; 
	   end;
       run;

%mend;

%dis_status(20220608, 20221019);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/30435"&gt;@lydiawawa&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I made a typo in the code:&lt;/P&gt;
&lt;P&gt;Following is the corrected version:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;%macro dis_status(startdate,enddate);
%let sasstart = %sysfunc(inputn(%substr(&amp;amp;startdate,3,6),yymmdd6.));
%put &amp;amp;sasstart;
%let sasend = %sysfunc(inputn(%substr(&amp;amp;enddate,3,6),yymmdd6.));
%put &amp;amp;sasend;

%do sasdate=&amp;amp;sasstart %to &amp;amp;sasend;
	%let date= %sysfunc(putn(&amp;amp;sasdate,yymmdd6.));
	%put &amp;amp;date;

     data master2;
       set master1;
		if code22_&amp;amp;date. in ('1','2','3','4') 
				then status22_&amp;amp;date. = "G "; 
		else if CODE22_&amp;amp;date. in ('5','6') 
		                                 then status22_&amp;amp;date. = "IE"; 
		else if CODE22_&amp;amp;date. in ('7','8') 
				then status22_&amp;amp;date. = "NR"; 
		else CODE22 in ('10,'11')
				then status22_&amp;amp;date. = "UE"; 
       run;
%end;
%mend;

%dis_status(20220608,20221019);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Feb 2023 18:23:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-create-multiple-variables-with-date-suffix-in-a-do-loop/m-p/859971#M339740</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-21T18:23:58Z</dc:date>
    </item>
  </channel>
</rss>

