BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

46 REPLIES 46
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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.

PaigeMiller
Diamond | Level 26

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?)

--
Paige Miller
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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?)

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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.;

Reeza
Super User

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
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

art297
Opal | Level 21

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


Quentin
Super User

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.

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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

SASKiwi
PROC Star

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;

art297
Opal | Level 21

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


Do you really want (?):


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

woo
Lapis Lazuli | Level 10 woo
Lapis Lazuli | Level 10

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?

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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