BookmarkSubscribeRSS Feed
forumsguy
Fluorite | Level 6

Hello,

I have a dataset close to 1 million observations. My task is to read only few observations

data have;

input name $ amt run_flag;

cards;

John 100 1

Peter 200 0

Serge 300 1

Mark 400 0

Jonathan 500 0

;

run;

%macro read_rec (ds);

%let table_id=%sysfunc(open(&ds(where=(run_flag=1)),i));

%let rc=%sysfunc(fetch(&table_id ));

%if &rc ne 0 %then

    %do;

    %put something is wrong ...;

    %end;

%else

    %do;

    %let name_code=%sysfunc(getvarc(&table_id, %sysfunc(varnum(&table_id,name))));

    %put name is &name_code;

    %end;

%let rc1 = %sysfunc(close(&table_id));

%mend;

%read_rec(have);

so I would need my macro to run 2 times for values John and Serge. But t runs only for John. I would have used datastep but this is a modification code so I am stuck to this, also since my original dataset is hug, i must use open and close functions... So my problem is to read one by one all matching conditions.. Please suggest where i am going wrong

2 REPLIES 2
Oleg_L
Obsidian | Level 7

You should make a loop. Something like this (untested):

%macro read_rec (ds);

%let j=1;

%let table_id=%sysfunc(open(&ds(where=(run_flag=1)),i));

%let rc=%sysfunc(fetch(&table_id ));

%do %while(&rc=0);

   %let name_code=%sysfunc(getvarc(&table_id, %sysfunc(varnum(&table_id,name))));

    %put name is &name_code;

    %end;

%let rc=%sysfunc(fetch(&table_id ));

%let j=%eval(&j+1);

%end;

%let rc1 = %sysfunc(close(&table_id));

%mend;

Vladislaff
SAS Employee

%macro read_rec (ds);

%let table_id=%sysfunc(open(&ds(where=(run_flag=1)),i));

%do %while (%sysfunc(fetch(&table_id))=0);

  %let name_code=%sysfunc(getvarc(&table_id, %sysfunc(varnum(&table_id,name))));

  %put name is &name_code;

%end;

%let rc1 = %sysfunc(close(&table_id));

%mend;

%read_rec(have);

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2 replies
  • 1158 views
  • 0 likes
  • 3 in conversation