To check error in dataset with array variables

New Contributor
Posts: 2

To check error in dataset with array variables

I have data with many array variables. I need to check if the values of the variables in the array meet certain conditions. I am not sure how this can be done using a macro.

eg: data ds1; set ds1;

array a1[*] a1_1- a1_100;

array a2[*] a2_1 -a2_100;


array a4[*] a4_1-a4_100;



I would like to check if any of the a1, a2,...a10 is empty or negative. and for some >1...If so then the macro would stop and give error.

I am using the following in a macro:

%macro test(dsin=ds1);

   data &dsin1;
  set &dsin1;
         %do i=1 %to 100;
        if ((a1_&i=.) or (a1_&i <0) or (a1_&i>1)) then stop_a1=1;
        if ((a2_&i=.) or (a2_&i <0)) then stop_a2=1;
        if ((a3_&i=.) or (a3_&i <0) or (a3_&i>1)) then stop_a3=1;
        if ((a4_&i=.) or (a4_&i <0) or (a4_&i>1)) then stop_a4=1; ;
     call symput("error_a1", stop_a1);
     call symput("error_a2", stop_a2);
     call symput("error_a3", stop_a3);
     call symput("error_a4", stop_4);

  if ((discount_rate)=. or (discount_rate<0) or (discount_rate>1)) then stop_var=1;
   call symput("error_var", stop_var);


 %if error_a1=1 %then %do; %put Error: a1 vector error; %end;
  %if error_a2=1 %then %do; %put Error: a2 vector error; %end;
 %if error_a3=1 %then %do; %put Error:a3 vector error; %end;
 %if error_a4=1 %then %do; %put Error: a4 vector error; %end;
  %if error_var=1 %then %do; %put Error: Check the data; %end;



I know there is error in the data set sdin1, but it is not giving me the error message. I am not sure what I am doing wrong here.

Also any other better way to do this?

Many thanks!

Super User
Super User
Posts: 8,069

Re: To check error in dataset with array variables

Before trying to work on a macro figure out what actual SAS code you want to generate.

There is no need for macro code to work with arrays.

It is not clear what the goal of the code is but if it is just to set macro variables then use a DATA _NULL_ step.

Perhaps you want to stop at the first error?

data _null_;
  set ds1 end=eof;
  array a1[*] a1_1- a1_100;
  retain any_errors 0;
  do i=1 to dim(a1) while (any_errors=0);
    if  not ( 0<= a1(i) <= 1) then any_errors=1;
  if any_errors or eof then call symputx('any_errors',any_errors);
  if any_errors then stop;
Super User
Super User
Posts: 9,599

Re: To check error in dataset with array variables

Rather than start by telling us what technology should be used, post some example test data in the form of a datastep:


This is the basis for any coding, and it is very hard to help without seeing it.


Then post what the output should look like based on this test data.  


I would start by saying that 4 arrays of 100 variables = at least 400 variables in one dataset, is pretty bad data modelling.  Sounds like the "Excel way of thinking".  Normalise - make your data go down the page, generally leads to smaller storage thus faster access, and simpler programming.

New Contributor
Posts: 2

Re: To check error in dataset with array variables

Thanks for the reply. I will make a more concrete example and post it.

Super User
Posts: 13,498

Re: To check error in dataset with array variables

A very brief example writing a specific message for type of "error".

data example;
   input x1 - x4;
   array x x1-x4;
   do i= 1 to dim(x);
      if x[i]=. then Put "For X array Index" +1 i +1 "on record" +1 _n_ +1 'is missing';
      if x[i]=0 then Put "For X array Index" +1 i +1 "on record" +1 _n_ +1 'is zero';
1 . 3 4
0 . 4 5

You could use the VNAME function to get the name of the variable instead of the indirect reference of the array index. Which may be important if the "some variables" with different ranges are in different groups of variables  such as A1_23 A2_18 and A4_99.



Use of FILE PRINT or FILE statement with a text file reference would send the output to something nicer than the log, which %put is a bit more difficult to arrange.

Ask a Question
Discussion stats
  • 4 replies
  • 4 in conversation