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.
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;
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.
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.
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?)
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?)
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
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.;
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):
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
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
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.
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
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;
I, too, don't understand why you are using _n_.
Do you really want (?):
if date ge (today()-2) then output;
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?
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.