Hopefully somebody can help me with my problem.
For the company I work for, I need to check if there is any data available in the dataset which is delivered monthly to me. If there is no data available then SAS has to stop the script and give an error (this is not the problem, the problem is before that part). The problem is as follows: We work with a very large dataset. New data is compared to old data so the new data and the 11 months before that are checked. My fellow epidemiologists and I write down the months we want to check in a predesigned macro-variable called 'deliveryfile'. This cannot be changed so this macrovariable needs to be included in the script. I want to make a loop that checks if a month (or months) is present in a dataset. If it is present, nothing will happen. If it is not present then the month is added to the pre-existing dataset.
Example dataset and macrovariable:
DATA existingdataset;
INPUT yearmonth total;
DATALINES;
202108 400
202109 0
202110 450
;
RUN;
%LET deliveryfile = 202110, 202111;
With the code above, I need to write a script which can check if month 202110 and 202111 are already present in the existingdataset. 202110 is present, so nothing will happen. 202111 is not present so it needs to be added to 'existingdataset'. Just the month needs to be added, the variable 'total' would be empty/NULL.
This is what I came up with, but I got several errors, most have to do with how I use PROC SQL. Number 1:
PROC SQL NOPRINT;
SELECT DISTINCT yearmonth INTO :yearmonth SEPARATED BY ', ' FROM existingdataset;
QUIT;
%PUT NOTE: yearmonth : &yearmonth; /*For verification */
%MACRO ADDING;
DATA existingdataset;
SET existingdataset;
IF
%DO i=1 %TO %SYSFUNC(COUNTW("&deliveryfile "));
%LET ymcontrol=%SCAN(%QUOTE(&deliveryfile ),&i,%STR( ));
&ymcontrol NOT IN (&yearmonth)
%END
THEN;
PROC SQL;
INSERT INTO existingdataset
SET yearmonth= &ymcontrol;
QUIT;
END;
RUN;
%MEND ADDING;
