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.
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.
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?
@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.
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.
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.
"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_value | fico | date |
1234 | 12.5 | Thu, Feb26, 2015 |
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;
I am sorry Arthur "date" variable is numeric with weekdate17. format.
Have you tried Quentin's solution. From what I understand, it should work for your situation.
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.
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.
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?
Did you run proc contents on all 10 datasets?
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 ;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.