<?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: Macro to check range of variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-check-range-of-variables/m-p/391690#M94140</link>
    <description>&lt;P&gt;Have you looked at the dataset rangetble? There is a chance that the forum has reformatted you text but when I run the code as copy and pasted from the window I get a value of "usmil not" for the first errorvar. I don't have the option expandtabs set and it appears that you have some tabs in the line so that may be a minor bit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also want to consider using some of the features that have appeared since 1999 such as Call symputx which removes leading and trailing blanks of the value so you can reduce the trim(left( code.&lt;/P&gt;
&lt;P&gt;Also insted of || there is a nice selection of concatenation functions. and Since Count = _n_ in the data _null_ for creating the macro variables as used why add the variable? That approach might be needed if you were doing something with repeaded values but not here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
set work.rangetbl end=last;
retain count;
	if _n_=1 then count=0;
	count=count+1;
	call symputx (cats('id',_n_),errorid);
	call symputx (cats('var',_n_),errorvar);
	call symputx (cats('code',_n_),errorsas);
   if last then call symputx('checkct',_n_);
run;&lt;/PRE&gt;
&lt;P&gt;If you are interested in seeing what the macro variables look like use: %put _user_; to see all of your created variables in the current scope.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also when you do this:&lt;/P&gt;
&lt;PRE&gt;data error_ds (keep=medid errorid errorvar errorval);
set dataset;
length errorid errorvar $8 errorval 8.;
%macro runcheck; /* &amp;lt;= %macro ends the previously started data set*/
	%do j=1 %to &amp;amp;checkct;
	%checkvar (%str (&amp;amp;&amp;amp;id&amp;amp;j),
				%str (&amp;amp;&amp;amp;var&amp;amp;j)    /*&amp;lt;=missing ,*/
				%str (&amp;amp;&amp;amp;code&amp;amp;j));
			%end;
	%mend runcheck;
&lt;/PRE&gt;
&lt;P&gt;the macro definition ends the data step as a boundary like a Run statement or a Proc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Define the macro before the data step that wants to use it. AND heres likely the fatal part: you are missing a , after the VAR parameter call to %checkvar&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;%macro runcheck;
	%do j=1 %to &amp;amp;checkct;
	%checkvar (%str (&amp;amp;&amp;amp;id&amp;amp;j),
				 %str (&amp;amp;&amp;amp;var&amp;amp;j),
				%str (&amp;amp;&amp;amp;code&amp;amp;j));
   %end;
%mend runcheck;

data error_ds (keep=medid errorid errorvar errorval);
   set dataset;
   length errorid errorvar $8 errorval 8.;
	%runcheck;
run;
&lt;/PRE&gt;</description>
    <pubDate>Tue, 29 Aug 2017 23:28:40 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-08-29T23:28:40Z</dc:date>
    <item>
      <title>Macro to check range of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-check-range-of-variables/m-p/391676#M94133</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would like some help trouble shooting a macro.&amp;nbsp; I found this&amp;nbsp; macro online for range checking my variables.&amp;nbsp; I know there are a lot of ways to range check, but I am hoping to be abl eto build a spreadsheet to not values outside of my acceptable ranges.&amp;nbsp; This one seems to work with that concepts, but I cannot get the macro to work with my sample data.&amp;nbsp; I've also attached the pdf with the macro explained by the author.&amp;nbsp; Any help is greatly appreciated!!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data rangetbl;
input @1 errorid $2.
	@3 errorvar $12.
	@15 errorsas $40.;
datalines;
1  usmil	  not(usmil in: (0,1))
2  usciv 	  not(usciv in: (0,1))
3  nonusmil   not(nonusmil in: (0,1))
4  nonusciv   not(nonusciv in: (0,1))
5  EPW 	      not (EPW in: (0,1))
;
run;

data dataset;
input
@1 medid 3.
@4 usmil 3.
@7 usciv 3.
@10 nonusmil 3.
@13 nonusciv 3.
@16 EPW 3.;
datalines;
1	0	0	1	1	2
2	1	0	1	0	0
3	3	1	2	0	0
4	0	2	1	0	0
;
run;

proc sort data=dataset;
by medid;
run;



data _null_;
set rangetbl end=last;
retain count;
	if _n_=1 then count=0;
	count=count+1;
	call symput ('id'||left (trim(put(count,5.))),
		trim (errorid));
	call symput ('var'||left (trim(put(count,5.))),
		trim (errorvar));
	call symput ('code'||left (trim(put(count,5.))),
		trim (errorsas));
if last then call symput
	('checkct',left(trim(put(count,8.))));
run;


%macro checkvar (id, var, check);
if &amp;amp;check then do;
	errorid="&amp;amp;id.";
	errorvar="&amp;amp;var.";
	errorval=&amp;amp;var;
	output;
	end;
%mend checkvar;

data error_ds (keep=medid errorid errorvar errorval);
set dataset;
length errorid errorvar $8 errorval 8.;
%macro runcheck;
	%do j=1 %to &amp;amp;checkct;
	%checkvar (%str (&amp;amp;&amp;amp;id&amp;amp;j),
				%str (&amp;amp;&amp;amp;var&amp;amp;j)
				%str (&amp;amp;&amp;amp;code&amp;amp;j));
			%end;
	%mend runcheck;
	%runcheck;
	run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2017 22:09:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-check-range-of-variables/m-p/391676#M94133</guid>
      <dc:creator>jenim514</dc:creator>
      <dc:date>2017-08-29T22:09:44Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to check range of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-check-range-of-variables/m-p/391690#M94140</link>
      <description>&lt;P&gt;Have you looked at the dataset rangetble? There is a chance that the forum has reformatted you text but when I run the code as copy and pasted from the window I get a value of "usmil not" for the first errorvar. I don't have the option expandtabs set and it appears that you have some tabs in the line so that may be a minor bit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may also want to consider using some of the features that have appeared since 1999 such as Call symputx which removes leading and trailing blanks of the value so you can reduce the trim(left( code.&lt;/P&gt;
&lt;P&gt;Also insted of || there is a nice selection of concatenation functions. and Since Count = _n_ in the data _null_ for creating the macro variables as used why add the variable? That approach might be needed if you were doing something with repeaded values but not here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
set work.rangetbl end=last;
retain count;
	if _n_=1 then count=0;
	count=count+1;
	call symputx (cats('id',_n_),errorid);
	call symputx (cats('var',_n_),errorvar);
	call symputx (cats('code',_n_),errorsas);
   if last then call symputx('checkct',_n_);
run;&lt;/PRE&gt;
&lt;P&gt;If you are interested in seeing what the macro variables look like use: %put _user_; to see all of your created variables in the current scope.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also when you do this:&lt;/P&gt;
&lt;PRE&gt;data error_ds (keep=medid errorid errorvar errorval);
set dataset;
length errorid errorvar $8 errorval 8.;
%macro runcheck; /* &amp;lt;= %macro ends the previously started data set*/
	%do j=1 %to &amp;amp;checkct;
	%checkvar (%str (&amp;amp;&amp;amp;id&amp;amp;j),
				%str (&amp;amp;&amp;amp;var&amp;amp;j)    /*&amp;lt;=missing ,*/
				%str (&amp;amp;&amp;amp;code&amp;amp;j));
			%end;
	%mend runcheck;
&lt;/PRE&gt;
&lt;P&gt;the macro definition ends the data step as a boundary like a Run statement or a Proc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Define the macro before the data step that wants to use it. AND heres likely the fatal part: you are missing a , after the VAR parameter call to %checkvar&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;%macro runcheck;
	%do j=1 %to &amp;amp;checkct;
	%checkvar (%str (&amp;amp;&amp;amp;id&amp;amp;j),
				 %str (&amp;amp;&amp;amp;var&amp;amp;j),
				%str (&amp;amp;&amp;amp;code&amp;amp;j));
   %end;
%mend runcheck;

data error_ds (keep=medid errorid errorvar errorval);
   set dataset;
   length errorid errorvar $8 errorval 8.;
	%runcheck;
run;
&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Aug 2017 23:28:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-check-range-of-variables/m-p/391690#M94140</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-08-29T23:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to check range of variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-check-range-of-variables/m-p/391892#M94213</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; THANK YOU so much!  It worked perfectly with your suggestions!</description>
      <pubDate>Wed, 30 Aug 2017 15:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-check-range-of-variables/m-p/391892#M94213</guid>
      <dc:creator>jenim514</dc:creator>
      <dc:date>2017-08-30T15:19:21Z</dc:date>
    </item>
  </channel>
</rss>

