Desktop productivity for business analysts and programmers

Macro help please

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 318
Accepted Solution

Macro help please

I am running below code and getting error -

date value for "test_check" dataset is Mon, Feb 23, 2015.

output - would be - number of datasets (ana&i.) where date value is not today()-2

options symbolgen merror serror spool;

%macro test;

%do i=1 %to 10;

   libname rep&i. "f:\woo\dir&i.\test_datasets";

data ana&i.;

set rep&i..test_check;

%if "date"d ne %sysfunc(today()worddate.)-2 %then %do;

   output ana&i.;

   %end;

run;

%end;

%mend;

%test;

ERROR: Expected close parenthesis after macro function invocation not found.

ERROR: Required operator not found in expression: "date"d ne

       %sysfunc(today()worddate.)-2

ERROR: The macro CHECK will stop executing.


Accepted Solutions
Solution
‎03-02-2015 03:24 PM
Grand Advisor
Posts: 17,447

Re: Macro help please

12 year old unsupported software isn't a great idea in general.

Try this:

data test ;

  length indsname dsname $256 ;

  set

       rep1.test_check (in=a)

       rep2.test_check (in=b)

       rep3.test_check (in=c)

  ;

  where date ne today()-2 ;

dsname=a*1+b*2+c*3 ; /*1=rep1, 2=rep2, 3=rep3*/

run;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,504

Re: Macro help please

There are lots of problems here.


What is the format of "date" ? Is it a true SAS date value or a character string or something else?

What is %check ?

You don't need %sysfunc anyway, you could just use today() without any macro functions.

Super Contributor
Super Contributor
Posts: 318

Re: Macro help please

What is the format of "date" ? Is it a true SAS date value or a character string or something else? date=Mon, Feb 23, 2015

What is %check ? - by mistake matched with another macro - deleted

You don't need %sysfunc anyway, you could just use today() without any macro functions.

Trusted Advisor
Posts: 1,504

Re: Macro help please

So Date is a character string, is that what you are saying?

And it uses month abreviations (Feb instead of February)?

And it uses day abbreviations (Mon instead of Monday) followed by a comma and a space?

(Or is date a SAS data value formatted as a character string?)

Super Contributor
Super Contributor
Posts: 318

Re: Macro help please

So Date is a character string, is that what you are saying? - date variable is "date type data of-course

And it uses month abreviations (Feb instead of February)? date value is = Mon, Feb 23, 2015 (i think we can use weekdate17.)

And it uses day abbreviations (Mon instead of Monday) followed by a comma and a space?

(Or is date a SAS data value formatted as a character string?)

Trusted Advisor
Posts: 1,504

Re: Macro help please

Okay, you use weekdate17.

Then your original problem is solved like this

%macro test;

%do i=1 %to 10;

   libname rep&i. "f:\woo\dir&i.\test_datasets";

data ana&i.;

set rep&i..test_check;

if date ne (today()-2) then output;

run;

%end;

%mend;

%test

Super Contributor
Super Contributor
Posts: 318

Re: Macro help please

i tried this but i am getting all 10 sas datasets (ana1 to ana10) where some datasets doens't has any value for variable "date" and some datasets (2 actually) where has value for "date" variable which is satisfying "IF" condition.

Isn't it we supposed to get only those 2 datasets where date value is not matching.

if date ne (today()-2) then output ana&i.;

Grand Advisor
Posts: 17,447

Re: Macro help please

Your macro checks against the variable values "DATE"  in a dataset. If no values are present the dataset is created, but is empty.

You need to add a step to not create the data step or delete it if the number of observations are 0.

This would go something like (pseudo code):


  • Check for values of date in dataset using proc sql
  • if count of obs>0 then proceed with data step as above code. (macro logic, i.e. %if)
  • no else required
  • end loop
Super Contributor
Super Contributor
Posts: 318

Re: Macro help please

i tried with this code but it is giving syntax error////

%macro test;

%do i=1 %to 10;

   libname rep&i. "f:\woo\dir&i.\test_datasets";

data ana&i.;

set rep&i..test_check;

if date ne (today()-2) and if _n_>0 then output;

run;

%end;

%mend;

%test

Esteemed Advisor
Posts: 7,300

Re: Macro help please

One definite syntax error will come from:

if date ne (today()-2) and if _n_>0 then output;


if date ne (today()-2) and _n_>0 then output;

will work better


Trusted Advisor
Posts: 1,216

Re: Macro help please

Actually, what is the point of the _n_  > 0 part?

_n_ is an counter for how many times the data step loop has iterated.  In your example, it will always be > 0.

If you are trying to implement Reeza's suggestion, that won't do it.  You would need an %IF statement (as he mentioned), to test whether or not there are 0 records that meet your criterion, and then only generate the data step code if there are > 0 records.

Super Contributor
Super Contributor
Posts: 318

Re: Macro help please

in this case,

if date ne (today()-2) and _n_>0 then output;


it is giving me all 10 datasets with zero obs.


Actually i want output only if date value (which is Wed,Feb25,2015) is less than 2 days back

Thank you

Respected Advisor
Posts: 3,066

Re: Macro help please

If I understand you correctly would this do what you want - only dates more recent than 2 days ago will be output.

if date > (today()-2) and _n_>0 then output;

Esteemed Advisor
Posts: 7,300

Re: Macro help please

I, too, don't understand why you are using _n_.


Do you really want (?):


if date ge (today()-2) then output;

Super Contributor
Super Contributor
Posts: 318

Re: Macro help please

First of all appreciate your time to help me.

Actually it is still giving me all 10 datasets from 10 directories with zero observation on it.

Let me conform i have "test_check" dataset in 10 different directory with one variable "date" which has value "Wed, Feb25, 2015". For now i have same value for all datasets so basically output of above code logic would be nothing- none dataset should be created since "if" condition is not satisfying.

is this help?

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 46 replies
  • 1069 views
  • 6 likes
  • 8 in conversation