DATA Step, Macro, Functions and more

Base SAS

Accepted Solution Solved
Reply
Contributor
Posts: 57
Accepted Solution

Base SAS

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


Accepted Solutions
Solution
‎09-06-2017 08:13 AM
Super User
Posts: 6,544

Re: Base SAS

Posted in reply to Siddharth123

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


All Replies
Solution
‎09-06-2017 08:13 AM
Super User
Posts: 6,544

Re: Base SAS

Posted in reply to Siddharth123

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.

Super User
Super User
Posts: 7,860

Re: Base SAS

Posted in reply to Siddharth123

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.

 

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 101 views
  • 0 likes
  • 3 in conversation