mustafghan
mustafghan

Reputation: 171

Creating datasets within Macro SAS

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?

Upvotes: 0

Views: 2537

Answers (1)

Haikuo Bian
Haikuo Bian

Reputation: 906

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

Upvotes: 2

Related Questions