Learning SAS? Welcome to the exclusive online community for all SAS learners.

Data step adding a variable when the condition is false also

Reply
Occasional Contributor
Posts: 17

Data step adding a variable when the condition is false also

Hi Experts,

I am trying to perform conditional merging. Following is my code:

%let market_name = "AUS";

data airsales;

      market = symget('market_name');

      if market = "AUS" then

        set airsales_temp;   

      if market = "NZL" then    

       do;

        merge airsales_temp

                  branch;

        by branch;

        end;

drop market;

run;

The above code is not merging the tables but is appending the variable even when the value is "AUS". The other work around that I could think about was dropping the added variable conditionally but that is also giving me error. Please help as to how should I solve this.

Regards,

Gurpreet

Super User
Posts: 11,343

Re: Data step adding a variable when the condition is false also

Posted in reply to gurpreetkaur

Lots of details about how the SAS data vector is built but since you want to conditionally create code then going a full macro route to allow macro conditional processing is an option:

%macro MyMarket (market=);

data airsales;

%if &market=AUS %then %do;

     set airsales_temp;

%end;

%if &market = NZL %then %do;

     merge airsales_temp

                branch;

     by branch;

%end;

run;

%mend;

Use:

%MyMarket(market=AUS)

or

%MyMarket(market=NZL)

Super User
Posts: 19,817

Re: Data step adding a variable when the condition is false also

Posted in reply to gurpreetkaur

Set and Merge are not conditional statements. You need to use macro logic (%if/%then) inside a macro instead.

Or you could simplify your logic a bit, though this is a very bad example. The key idea is that BY and MERGE don't require the parameters which make flexible for dynamic programming.

%let market_name=NZL;

%let merge_set = %sysfunc(ifc(&market_name=NZL, branch, %str()));

%put &merge_set;

data want;

merge class &merge_set;

by &merge_set;

run;

Ask a Question
Discussion stats
  • 2 replies
  • 264 views
  • 0 likes
  • 3 in conversation