Hi everybody
I'm trying to extract a subset of variables from some locked datafiles with a macro. I do only have reading access to the folder where i'm importing my data from.
My syntax looks like this:
%macro extract_bef(start, end);
%do year = &start. %to &end.;
data rawdata.bef&year.;
Merge ekstern.&pop (in = A)
data.bef&year. (in = B keep = &BEF.);
by ID;
if A and B;
run;
%end;
%mend;
%extract_bef(201312,201812)
This works fine, but my problem is that I get a lot of datafiles that I really dont need. I want to extract some defined variables in the macro &bef from year 2013 until 2018. But in my output datafolder I also gets the "years" 201313, 201314, 201315..... 201399 and etc. Really frustrating. It is not a opportunity for me to change the names from bef201312 to bef2013 etc. in the input datafolder because I do only have reading access. Does anyone have an idea how to solve this?
In a basic datastep my syntax would have looked liked this:
data bef2013;
set bef201312 (keep = &BEF.;
run;
Instead of doing this multiple times i'm trying to solve this with the above macro &extract_bef.
Hope there is someone who can help me fix this.
Kind regards
Frank
Then you should supply only the years as parameters:
%extract_bef(2013,2018)
and change the MERGE to
merge
ekstern.&pop (in = A)
data.bef&year.12 (in = B keep = &BEF.)
;
Do you only want the YYYY12 datasets , or all "official" months (01-12) for a given year?
Then you should supply only the years as parameters:
%extract_bef(2013,2018)
and change the MERGE to
merge
ekstern.&pop (in = A)
data.bef&year.12 (in = B keep = &BEF.)
;
The idea of breaking up a large data set into smaller pieces (by month) is usually not a good one and should be avoided. Many of the things you might want to do are essentially easier with one large data step and BY statements.
To address your specific problem
But in my output datafolder I also gets the "years" 201313, 201314, 201315..... 201399 and etc.
Your DO loop will go from 201312 to 201812 BY 1 where the BY 1 is implied, even though you leave the BY 1 out, and so after 201312 comes 201313. This is simple math. SAS does not know that you intend the integer 201312 to indicate a year/month, it thinks it is just a plain integer. So what you should do is turn 201312 into an actual SAS date value, and then increment by one month intervals using the INTNX function.
But really, leave this as one big data set. Don't break it up. Use BY statements to analyze the individual months. So much simpler than writing macros to split it up, and then writing more macros to perform analyses on a month by month basis. And doing it this way also avoids the problem you are having where you get additional months that don't exist. A word to the wise, you know. (If you need help doing it with one big data set and BY statements, please ask)
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.