07-15-2013 04:43 AM
i'm not sure if this is what you're looking for.
proc sql noprint;
select count(*) into bservations
%do i=1 %to &observations;
set sashelp.class (firstobs=&i obs=&i);
putlog "Loop: &i do something";
07-15-2013 05:50 AM
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?
07-15-2013 09:43 AM
Sometimes you need to complete your homework per the instructions...
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:
%local dsid rc now;
%let dsid = %sysfunc(open(dataset));
%do %while (%sysfunc(fetch(&dsid)) = 0); /* outer loop across rows;*/
%end; /*while fetch loop*/
You can use getvarn or getvarc to retrieve values the current row of &dsid but you have to wrap them in %sysfunc().
07-15-2013 10:49 PM
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?
07-16-2013 03:23 AM
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
07-16-2013 06:14 PM
OPEN, CLOSE, and FETCH are not solely MACRO functions. They can also be used within a datastep,as per below:
/* CREATE VARIABLES FOR ASSIGNMENT */
/*BY CALL SET */
LENGTH NAME $8 SEX $1 AGE WEIGHT HEIGHT 8;
DROP RC DSID;
CALL SET (DSID);
DO I=1 TO 10;
07-17-2013 01:53 AM
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
07-17-2013 02:07 AM
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.
07-16-2013 08:28 AM
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.