BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Kjeldsen89
Calcite | Level 5

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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

View solution in original post

4 REPLIES 4
Kjeldsen89
Calcite | Level 5
Hallo Kurt

I only want the YYYY12 datasets for a given year.
Kurt_Bremser
Super User

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.)
;
PaigeMiller
Diamond | Level 26

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)

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

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
  • 4 replies
  • 681 views
  • 0 likes
  • 3 in conversation