Help using Base SAS procedures

Subsetting a dataset using Macro

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Subsetting a dataset using Macro

Hi All,

I am having issue while subsetting a dataset using Macro.

here is my code :

%macro dt (value);

data raw_new;

set raw;

where index(score, "&value");

%dt(apple_1);

%dt(orange_2);

run;

From Raw data set I want to create raw_new data set. Variable score has 1000 values and I want to keep only those values which contains "apple_1" and "orange_2". Every time I run this code, I get only values with orange_2 in my dataset. apple_1 does not get appended to orange_2. Could you please suggest what I'm doing wrong.

Thank You!


Accepted Solutions
Solution
‎04-24-2014 07:25 AM
Super Contributor
Posts: 282

Re: Subsetting a dataset using Macro

Posted in reply to Pujasharma

Hi,

The macro starts ok, but if you always write to the same data set (raw_new) each time you invoke the macro function %dt then raw_new will be overwritten each time, the last time being with orange_2 data which might be why you're not seeing apple_1 data.

Try something like (untested):

%macro dt (value);

  data raw_new_temp;

    set raw;

    where index(score, "&value");

  run;

  proc append base=raw_new

              data=raw_new_temp

              force;

  run;

%mend dt;

%dt(apple_1);

%dt(orange_2);

The above code creates a temporary data set then appends it to raw_new.

Regards,

Amir.

Message was edited by: Amir - corrected reference to raw_new.

View solution in original post


All Replies
Super User
Posts: 10,035

Re: Subsetting a dataset using Macro

Posted in reply to Pujasharma

where index(score, "&value");

--->

if index(score, "&value");

OR

where score contains "&value";

Solution
‎04-24-2014 07:25 AM
Super Contributor
Posts: 282

Re: Subsetting a dataset using Macro

Posted in reply to Pujasharma

Hi,

The macro starts ok, but if you always write to the same data set (raw_new) each time you invoke the macro function %dt then raw_new will be overwritten each time, the last time being with orange_2 data which might be why you're not seeing apple_1 data.

Try something like (untested):

%macro dt (value);

  data raw_new_temp;

    set raw;

    where index(score, "&value");

  run;

  proc append base=raw_new

              data=raw_new_temp

              force;

  run;

%mend dt;

%dt(apple_1);

%dt(orange_2);

The above code creates a temporary data set then appends it to raw_new.

Regards,

Amir.

Message was edited by: Amir - corrected reference to raw_new.

New Contributor
Posts: 2

Re: Subsetting a dataset using Macro

That worked, Amit! Thank you!

🔒 This topic is solved and locked.

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

Discussion stats
  • 3 replies
  • 260 views
  • 3 likes
  • 3 in conversation