BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Siddharth123
Obsidian | Level 7

Hello,

 

I am trying to do the following - Loop through a list of 5 variables in a given dataset and if any value in missing ( . or " ") Then Flag_1 = "ABORT" or ... Flag_5 = "ABORT"; Next, if any of the 5 flags is "ABORT" then PROC SQL prints message : ABORT, else PROCEED if all FLAGS are "PROCEED".

 

I am using the following code:

 

%macro Imports (Sheet, Out);

PROC  IMPORT  OUT= &Out.

DATAFILE=  "&library.\&Sheet..xlsx" DBMS=XLSX REPLACE;

GETNAMES=YES;

MIXED=NO;

RUN;

 

DATA &Out.; SET &Out.;

if Var1 = " "  then Flag = "ABORT"; ELSE Flag = "PROCEED";

RUN;

 

proc freq data = &Out. ; TABLES Flag/ OUT = Cont_&Out. LIST MISSING NOROW NOCOL NOCUM NOPERCENT;

RUN;

 

PROC SQL noprint;

SELECT DISTINCT FLAG INTO : FLAG2 separated BY " " FROM Cont_&Out.;

%if %INDEX(COMPRESS(&FLAG2.) , "ABORT") > 0 %then %DO ;%PUT ABORT: MISSING VAR1; %END;

%else %if %INDEX(COMPRESS(&FLAG2.) , "ABORT") = 0 %then %DO; %PUT PROCEED : VAR1 FULLY POPULATED;%END;

QUIT;

 

%put &FLAG2.;

%mend;

%Imports (dataset, dataset);

 

Please help above code gives error as it does not read the ABORT in INDEX function. Also, is it possible to have a loop / array within macro to loop through all 5 variables in dataset?

 

Kind Regards

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

What you are describing as your objective is a simple DATA step, not a complex macro.  The only complication to worry about is determining that you have both character and numeric variables in your data set.  Here's what the final program would look like, when you know you have a mix:

 

data _null_;

set have end=done;

array charvars {*} _character_;

array numvars {*} _numeric_;

do _n_=1 to dim(charvars);

   if charvars{_n_} = " " then do;

      put 'ABORT:  Found a Missing Character Variable';

      stop;

   end;

end;

do _n_=1 to dim(numvars);

   if numvars{_n_} = . then do;

      put 'ABORT:  Found a Missing Numeric Variable';

      stop;

   end;

end;

if done;

put 'PROCEED:  No Missing Values Found';

run;

 

The only tricky part that macro language might automate is constructing lists of which variables belong in which array.  Other than that, macro language doesn't come into play.  (If there are no numeric variables, for example, creating an array with _NUMERIC_ as the variable list will create an error condition.)  But we'll leave that for a separate question if you actually have to overcome that problem.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

What you are describing as your objective is a simple DATA step, not a complex macro.  The only complication to worry about is determining that you have both character and numeric variables in your data set.  Here's what the final program would look like, when you know you have a mix:

 

data _null_;

set have end=done;

array charvars {*} _character_;

array numvars {*} _numeric_;

do _n_=1 to dim(charvars);

   if charvars{_n_} = " " then do;

      put 'ABORT:  Found a Missing Character Variable';

      stop;

   end;

end;

do _n_=1 to dim(numvars);

   if numvars{_n_} = . then do;

      put 'ABORT:  Found a Missing Numeric Variable';

      stop;

   end;

end;

if done;

put 'PROCEED:  No Missing Values Found';

run;

 

The only tricky part that macro language might automate is constructing lists of which variables belong in which array.  Other than that, macro language doesn't come into play.  (If there are no numeric variables, for example, creating an array with _NUMERIC_ as the variable list will create an error condition.)  But we'll leave that for a separate question if you actually have to overcome that problem.

Tom
Super User Tom
Super User

I don't understand what you want.

It sounded like you want to stop if ANY of the variables have ANY missing values? 

Is that right?

 

%let abort=0;
data _null_;
  set have ;
  if cmiss(of _all_) then do;
    call symputx('abort',1);
   stop;
 end;
run;

 

Mixing in the PROC IMPORT step into the macro doesn't really help as I don't think that is part of your actual problem. You can always add that back later once you get code to do the tricky part.

 

 

 

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
  • 2 replies
  • 753 views
  • 0 likes
  • 3 in conversation