0
votes

I have the following code which checks to see if a filename URL statement has passed it's content to and infile statement correctly or not. If not the macro is supposed to iterate through ten attempts to make the URL connection then give up:

%macro test_exst(iter);

/*filename loader url "http://www.sdhsdisdhsidhsidh.com";*/
/*filename loader url "http://www.bbc.co.uk/sport";*/
filename loader url "http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=na5";

/*  data step based on filename url above goes here, each pass will give 500 metrics x 1 symbol dataset*/


%put create dataset from csv submission;


%Let ITER = %Eval(&ITER + 1);
%let obscount = 1;

data mytest;
infile loader length=len MISSOVER /*delimiter = ','*/;
input record $varying30. len; 
format record $30.;
informat record $30.;
call symputx("obscount2",_n_);
run;

%put obscount = &obscount;
%put obscount2 = &obscount2;

%if &obscount2= .    %then %let obscount2 = &obscount;

%put obscount2 = &obscount2;


 %if &obscount2=1 and %eval(&iter. < 10) %then %do;
   %put Iteration &iter. failed, trying again;
   %test_exst(&iter.);
 %end;
%mend test_exst;

%test_exst(0);

This works fine as long as the dataset is created correctly (i.e. the code does not iterate inappropriately), however when the filename URL connect does not work the macro variable &obscount2. is not resolved correctly and I get an error message saying that a character string has been passed to an %if or %eval statement incorrectly.

I understand why this error is occurring, but I can't seem to get the syntax correct for the line:

%if &obscount2= .    %then %let obscount2 = &obscount;

Basically here I am trying to say that if '&Obscount2' has not been resolved then make it equal '&obscount' instead, which has been pre defined as 1. Then at the line:

%if &obscount2=1 and %eval(&iter. < 10) %then %do;

I know my dataset will only ever have 200 or zero observations because of the nature of the way my URL will be coded in the final version of this code. If &Obscount2 = 1 here it can only be because it has been pointed to this by it not being resolved originally. The code will then loop until a filename URL connect is made.

Any ideas on how on how to complete the code?

Thanks

2

2 Answers

0
votes

You could create your dataset mytest and then use the attrn function to check how many observations it has, as per this link:

http://support.sas.com/kb/25/078.html

/* Sample data */

data one;
  input x;
datalines;
1
2
;


%let dsid=%sysfunc(open(one));
%let num=%sysfunc(attrn(&dsid,nlobs));
%let rc=%sysfunc(close(&dsid));

%put There are &num observations in dataset one.;
0
votes

This was what worked in the end:

%macro test_exst(iter);

/*filename loader url "http://www.sdhsdisdhsidhsidh.com";*/
/*filename loader url "http://www.bbc.co.uk/sport";*/
filename loader url "http://finance.yahoo.com/d/quotes.csv?s=GOOG&f=na5";

/*  data step based on filename url above goes here, each pass will give 500 metrics x 1 symbol dataset*/


%put create dataset from csv submission;


%Let ITER = %Eval(&ITER + 1);


data mytest;
infile loader length=len MISSOVER /*delimiter = ','*/;
input record $varying30. len; 
format record $30.;
informat record $30.;
run;

%let dsid=%sysfunc(open(mytest));
%let num=%sysfunc(attrn(&dsid,nlobs));
%let rc=%sysfunc(close(&dsid));

%let obscount = &num;

%put obscount = &obscount;

 %if &obscount=0 and &iter. < 10 %then %do;
   %put Iteration &iter. failed, trying again;
   %test_exst(&iter.);
 %end;
%mend test_exst;

%test_exst(0);