BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SASKiwi
PROC Star

Yes, if you only have one date in your data "Wed, Feb25, 2015" and you run your SAS program on 28 Feb then that is 3 days ago and you will get no data. I assume your real data will have varying dates otherwise there is no point trying to select on date as you would always get all observations or no observations - unless that is what you want.

Quentin
Super User

To avoid generating all 10 datasets, you need to use a macro %IF to control generation of the each data set, depending on whether or not there are any records.

Jack Hamilton wrote a really nice paper on function-style macros that count the number of a records in a dataset.  http://www2.sas.com/proceedings/sugi26/p095-26.pdf.

You can use his %MTANYOBS macro in your setting, like:

%macro test(nData);
  %local i;
  %do i=1 %to &nData;
    %if %mtanyobs(data=rep&i.(where=(date ne (today()-2)))) %then %do;
      data ana&i ;
        set rep&i ;
        if date ne (today()-2) then output ;
      run;
    %end;
  %end;
%mend;

data rep1;
  date=today();output;
  date=today()-1;output;
run;
data rep2;
  date=today()-2;output;
  date=today()-2;output;
run;
data rep3;
  date=today()-2;output;
  date=today()+1;output;
run;
 
%test(nData=3)

So that macro call will generate ana1 (with 2 obs) and ana3 (with 1 obs), but it will NOT generate ana2 (with 0 obs), because rep2 had 0 records that satisfied the where= option.

Hope that helps,

--Q.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
art297
Opal | Level 21

I'm confused! You said that "

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

in SAS it can only be character or numeric. Which is it?

woo
Barite | Level 11 woo
Barite | Level 11

@Arthur - its "date" type with weekdate17. format

@SASKIWI -  I am running code today, 27th Feb and value for "date" variable in dataset is "Wed, Feb25, 2015" so only 2 days back and not 3 days ago.

art297
Opal | Level 21

Can you zip and post one of your files. That would definitely make is easier for folks here to figure out what the problem is.

Tom
Super User Tom
Super User

Please clarify the requirements. I think you are asking two questions.

1) How to test the value of your date variable to the current date.

2) How to conditionally generate datasets based number of observations found.

To solve the first you need to be clear on the variable you are testing.  If it is a date value then you can compare to the TODAY() or DATE() function.  You could also compare to "&sysdate9"D to compare the date that the SAS program started.  If it is not a date variable then you will need to first convert it.

To solve the second problem you would need to resort to code generation (macro code, call execute, etc.), or if the data in question is small then you might be able to use a hash object to store the data and then conditionally generate the dataset from the hash object.

woo
Barite | Level 11 woo
Barite | Level 11

"test_check" dataset looks like this today in all 10 different directories. in some directory - "date" variable value is not same as I shown below so I want those datasets out.

ms_value        type=num

fico                  type=num

date                 type=date   

ms_valueficodate
123412.5Thu, Feb26, 2015
art297
Opal | Level 21

You keep saying that the type is 'date'.  Would you run the following code on your ten datasets and post the results for the forum to see:

proc contents data=have (keep=date) nodetails;

run;

woo
Barite | Level 11 woo
Barite | Level 11

I am sorry Arthur "date" variable is numeric with weekdate17. format.

Reeza
Super User

Have you tried Quentin's solution. From what I understand, it should work for your situation.

woo
Barite | Level 11 woo
Barite | Level 11

I am receiving below error with Quentin code:

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric

       operand is required. The condition was: %mtanyobs(data=rep&i.(where=(date ne

       (today()-4)))) 

ERROR: The macro TEST will stop executing.

Quentin
Super User

Re problems with my suggestion, were you able to compile Jack's macro and get it working?

I forgot Jack's macro returns a  . (dot) if the dataset cannot be found or cannot be opened.  So it's possible that is the problem.  Do you get any warnings in the log about datasets not existing? (or failure to open)

I assumed rep&i would always exist.  You could change to %if %mtanyobs(...) = 1 %then do;

Then if rep&i didn't exist it would not generate the step to make ana&i.

I haven't been following the discussion about what you want to do with the date variable, but I think the approach I suggested should work for conditional generation of datasets only if there is at least one record meeting some criterion.

BASUG is hosting free webinars Next up: Mike Sale presenting Data Warehousing with SAS April 10 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

Can I take a step back here.  What is it your trying to achieve, why do you have 10 libraries/10 files with one observation in each?  Would you not be better off putting those tables together with and identifier, then processing that, i.e. get away from the need for macro's at all.  I don;t see any reason why your logic needs to be in a macro?

art297
Opal | Level 21

Did you run proc contents on all 10 datasets?

Tom
Super User Tom
Super User

Why not just combine the data into a single data set?

%macro test;

data test ;

  length indsname dsname $256 ;

  set

%do i=1 %to 10;

  "f:\woo\dir&i.\test_datasets\test_check"

%end;

  indsname=indsname

  ;

  where date ne today()-2 ;

dsname=indsname ;

run;

%mend test ;

sas-innovate-2024.png

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.

 

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
  • 2203 views
  • 6 likes
  • 8 in conversation