DATA Step, Macro, Functions and more

Read Data Set through Do Loop.

Reply
Contributor
Posts: 29

Read Data Set through Do Loop.

Is that possible to read one by one observation from a data set through Do Loop?

My requirement is to traverse through the observations of the data set using loops.

Thank youSmiley Happy    

Super User
Posts: 10,018

Re: Read Data Set through Do Loop.

Not tested code;

data _null_;

do until(last);

  set sashelp.class end=last;

  n+1;

end;

put n=;

run;

Ksharp

Occasional Contributor
Posts: 10

Re: Read Data Set through Do Loop.

Hi,

i'm not sure if this is what you're looking for.

%macro loop;

proc sql noprint;

    select count(*) into Smiley Surprisedbservations

        from sashelp.class;

quit;

%do i=1 %to &observations;

    data _null_;

        set sashelp.class (firstobs=&i obs=&i);

        putlog "Loop: &i do something";

    run;

%end;

%mend;

%loop;

Super Contributor
Posts: 297

Re: Read Data Set through Do Loop.

I don't understand.  SAS reads and processes the observations sequentially in a datastep, so there is no need for a do loop.

Am I missing something here?

Super Contributor
Posts: 578

Re: Read Data Set through Do Loop.

Posted in reply to Scott_Mitchell

Sometimes you need to complete your homework per the instructions...Smiley Happy

Sometimes you need to open a dataset and iterate through the observations performing other functions on each row.  I don't think you can do this within a data step.  You can simulate a cursor within a macro statement:

%Macro M1(dataset);

%local dsid rc now;

%let dsid = %sysfunc(open(dataset));

%do %while (%sysfunc(fetch(&dsid)) = 0); /* outer loop across rows;*/

   

%end; /*while fetch loop*/

%mend;

You can use getvarn or getvarc to retrieve values the current row of &dsid but you have to wrap them in %sysfunc().

Super Contributor
Posts: 297

Re: Read Data Set through Do Loop.

I think you might be right about the homework to be honest.

I use OPEN And CLOSE frequently when I want to identify the number of OBS in a dataset, but I have never used FETCH.  I can see the benefits to create a Macro variable, but can you use these to write back to a dataset?

Valued Guide
Posts: 2,177

Re: Read Data Set through Do Loop.

Posted in reply to Scott_Mitchell

Macro functions cannot create or write to a SAS data set. Veryify this by looking in the  language documentation at the options available for the open() function

Super Contributor
Posts: 297

Re: Read Data Set through Do Loop.

Hi Peter,

OPEN, CLOSE, and FETCH are not solely MACRO functions.  They can also be used within a datastep,as per below:

DATA MYDATA;

      /* CREATE VARIABLES FOR ASSIGNMENT */

      /*BY CALL SET */

   LENGTH NAME $8 SEX $1 AGE WEIGHT HEIGHT 8;

   DROP RC DSID;

   DSID=OPEN("SASHELP.CLASS","I");

   CALL SET (DSID);

   DO I=1 TO 10;

      RC=FETCHOBS(DSID,I);

      OUTPUT;

   END;

RUN;

Valued Guide
Posts: 2,177

Re: Read Data Set through Do Loop.

Posted in reply to Scott_Mitchell

Scott

Of course you are correct.

What I should have pointed out is that OPEN() in the context requested provides only the functionality of the the data step environment through %SYSFUNC() and although the SAS/SCL environment provides a similar function which does support UPDATE and (iirc) NEW these open modes were not ported to the base SAS  environment along with several other SCL-type functions (way back in SAS release 6.09e days).

Which is a long winded way of saying

Answer your own question

Read the documentation for OPEN()  - there is no WRITE

Should the WRITE mode be needed, learn and use SAS/SCL

.

Super Contributor
Posts: 297

Re: Read Data Set through Do Loop.

Thanks Peter.  It is nice to hear about the history of these functions.  I only started with V8 and although I feel as though I have a decent grip on things, I certainly have a few of those "why is it so" moments.

Thanks again for clearing that up.

Super Contributor
Posts: 339

Re: Read Data Set through Do Loop.

Posted in reply to Scott_Mitchell

Hi Scott,

As an example as to why this can be useful from personal experience.  I've had to do a levenstein (complev) between all words of a "dictionary" and all words in a given column of a gigantic dataset. Building a proc FCMP taking a character array _TEMPORARY_ as a parameter (the dictionary) allowed me to do so in a single data step with very effective processing speed. In order to pass the said dictionary to the proc FCMP, I needed to do such a do loop on the dictionary dataset to load it in an array.

Vincent

Super Contributor
Posts: 297

Re: Read Data Set through Do Loop.

Posted in reply to Vince28_Statcan

Intriguing Vincent.  I look forward to having an opportunity to do that in the future.

Ask a Question
Discussion stats
  • 11 replies
  • 698 views
  • 3 likes
  • 7 in conversation