0

I have the following code:

%macro MSA (Data=, Code=, MSAName=);
    data &Data;
    set NoDup;
        %if MSA = &Code %then %do; 
        MSA_name = "&MSAName";
        output &data;
   %end; 
   run; 
   %mend MSA;
   %MSA (Data=Bakersfield, Code=12540, MSAName=Bakersfield);
   %MSA (Data=Chico,       Code=17020, MSAName=Chico);

So I get two datasets as I want with one names Bakersfield and another Chico. However, the MSA column does not display the correct value (i.e. a column of 12540 for Bakersfield and 17020 for Chico for MSA), nor do I get a variable named MSA_Name that gives me the correct values (Bakersfield for all of the MSA_Name column, and Chico). What am I doing wrong?

2
  • To be clear on what is happening: The macro statements simply generate the SAS statements that will be executed. MSA = 12540; is false (the macro processor doesn't access the variables in your data set, it just compares text), so the following code is executed: data Bakersfield; set NoDup; run;. You are just copying your data set. Commented Jun 29, 2015 at 18:56
  • You are exactly right. What is the solution? Commented Jun 30, 2015 at 11:58

1 Answer 1

2

The issues you have with your code is to mingle macro syntax with data step. Please try the following:

%macro MSA (Data=, Code=, MSAName=);
    data &Data;
    set NoDup;
        if MSA = &Code /*if MSA is char, you will need quote "&code"*/ then  do;
        MSA_name = "&MSAName"; output; end;
      run; 
   %mend MSA;
   %MSA (Data=Bakersfield, Code=12540, MSAName=Bakersfield);
   %MSA (Data=Chico,       Code=17020, MSAName=Chico);
Sign up to request clarification or add additional context in comments.

7 Comments

The result from this code wasn't any different from mine.
There is no %-sign in this solutions IF, THEN etc. Original code use %IF and it doesn't work when MSA is an observation not a macro variable. Sure there isn't any difference?
@user2916331 He took out your output statement. Use a do block like this instead: if MSA = "&Code" then do; MSA_name = "&MSAName"; output; end;.
@DWal, why you need an explicit 'output' when an implicit 'output' is in place and does the exact same thing?
Explicit OUTPUT is needed to make the output dataset a subset of the input data set. Otherwise the program is just copy the data and generating MSA_NAME for only some of the records.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.