Nest macros within or create automation for variables

Reply
Occasional Contributor
Posts: 8

Nest macros within or create automation for variables

How do I create a macro within a macro or some automation process to do the following  three checks to a master file that contains the following variables:

ID

b_ID

FLAG

MEM_ID (unique key)

var4--var9, etc.  (all text fields)

I would like to create a macro that checks for the following:

1. no missing fields in the file (see below)

    *Here is what I have so far for some of the checks...

          *check for missing value;

          %macro missing (filename);

                let var_list= ID b_ID Flag VAR4 VAR5 VAR6 ;

                        data masterfile;

                              set masterfile;

                        if &var_list = ' '; then output;

                        run;

          %mend missing;-ID must contain leading zeros and be 3 digits; b_ID must contain 7 digits

2. The b_ID variable must be 7 characters with the first beginning with a letter and the last 6 digits having only numbers

*check for leading zeros and length of variables and specific variable starts with a letter;

%macro digits (filename=, varname=, varlength=);  ** (confused where to put the length statement so when the macro runs, it checks the variable and the desired length and outputs the records that don't meet standard

    let var_list2= b_ID var6;

          data masterfile;

              set masterfile;

              where (substr(&varname,1,1) in ('A', 'B', 'C'));

          run;

%mend digits;

3.The flag variable can only be 'y', 'n' or '888'.

  *check for flag value;

    %macro flagvar(filename);

          let var_list= ID b_ID Flag VAR4 VAR5 VAR6 ;

          data masterfile;

              set masterfile;

              if &var_list not in ("Y", "N", "888"); then output;

          run;

    %mend flagvar;

SAMPLE DATA:

ID B_ID FLAG MEM_ID VAR4 VAR5 VAR6 VAR7 VAR8 VAR9

001 A123456 Y 125K52 20130204 1 999 1 1 999

003 B234567 Y 358L74 20130506 2 3 2 2 3

038 B987654 Y 125K53 20140206 3 2 5 3 2

096 A129867 N 358L75 20130608 3 1 4 3 1

054 B171121 888 125K54 20130712 3 1 3 3 1

005 B249328 N 358L76 20130204 4 1 1 4 1

034 A123458 N 125K55 20130506 999 1 1 999 1

017 B324615 888 358L77 20141205 888 999 5 888 999

039 B400002 Y 125K56 20120608 999 2 5 999 2

099 A123459 Y 358L78 20130612 2 1 999 2 1

Any guide in organizing these thoughts or input that I'm going in the right direction will be appreciated!

Thank you!

SAS Super FREQ
Posts: 8,743

Re: Nest macros within or create automation for variables

Hi:

  I think you are misunderstanding some of the basics of SAS macro processing. The rule of thumb is to start with a WORKING SAS program. You do not have a WORKING SAS program.

  So, for example, let me illustrate what happens when you try to execute a statement like this (which is similar to what your #1 macro program would resolve to):

if name sex = ' ' then output;

  Take a look at the attached screen shots. I do not believe any of your programs will work as you expect. You cannot for example have the macro facilty resolve code to this (as you want for your #3):

              if ID b_ID Flag VAR4 VAR5 VAR6 not in ("Y", "N", "888"); then output;

First of all the IF statement is not syntactically correct and second of all even if the IF were correct, you have the semi-colon in the wrong place and I doubt you want a subsetting IF for this syntax. Please see the attached screen shots.

cynthia


Problem_with_macro_code.pngstart_with_working_program.png
Super User
Super User
Posts: 6,502

Re: Nest macros within or create automation for variables

Use the CMISS() function to count the number of missing values in a list of variables.  No macro logic needed.

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

                  

data any_missing_values ;

   set masterfile ;

   if cmiss(of ID b_ID Flag VAR4 VAR5 VAR6) > 0 then output;

run;

Occasional Contributor
Posts: 8

Re: Nest macros within or create automation for variables

This worked! thank you very much...especially for the simplification!!

Ask a Question
Discussion stats
  • 3 replies
  • 184 views
  • 1 like
  • 3 in conversation