BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pujasharma
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

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

3 REPLIES 3
Ksharp
Super User

where index(score, "&value");

--->

if index(score, "&value");

OR

where score contains "&value";

Amir
PROC Star

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.

Pujasharma
Calcite | Level 5

That worked, Amit! Thank you!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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