BookmarkSubscribeRSS Feed
lekha123
Calcite | Level 5

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;

 

run;

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;
  stop_a1=0;
  stop_a2=0;
  stop_a3=0;
  stop_a4=0;
  stop_var=0;
         %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);
       %end;

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

 

 %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;

 
 %mend;

 

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!

4 REPLIES 4
Tom
Super User Tom
Super User

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;
  end;
  if any_errors or eof then call symputx('any_errors',any_errors);
  if any_errors then stop;
run;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

 

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.

lekha123
Calcite | Level 5

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

ballardw
Super User

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';
   end;
datalines;
1 . 3 4
0 . 4 5
;
run;

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 460 views
  • 2 likes
  • 4 in conversation