DATA Step, Macro, Functions and more

macro variable in set statements based on today()

Accepted Solution Solved
Reply
Valued Guide
Posts: 860
Accepted Solution

macro variable in set statements based on today()

I am trying to join a years worth of tables together while adding a field 'Reprting_Month'.  What I have isn't working, I'm hoping it'll make enough sense to get the point accross.  

 

What I have;

consumer201611

field1 field1

 

consumer201610

field1 field1

 

What I want:

 

Consumer_Agg

201611 field1 field2

201610 field1 field2.

 

What I've tried so far is somethig like this:

 

data consumer_agg;

%macro dates;

set

%do date = 201511 %to 201611;

fdr.consumer&dates;

%end;

%mend;

end;

 

the code runs with no errors but has no output.  I know I've done this in the past but can't find my old code.  


Accepted Solutions
Solution
‎12-22-2016 04:59 PM
Super User
Posts: 11,343

Re: macro variable in set statements based on today()

Posted in reply to Steelers_In_DC

Try:

%macro dates;
%do date = 201511 %to 201611;
fdr.consumer&dates
%end;
%mend;

data consumer_agg;

set
%dates
;
end;

3 problems: 1) you never called the macro

 

2) by putting ; after the fdr.consumer&dates value you would have terminated the set statement with the first name.

3) The data step ended on encountering the %macro statement as that is one of the boundaries between procedures. I suspect you got some odd message in the log about a data set with 0 variables.

 

Use

Options mprint symbolgen;

to see the code that is generated by your actual macro code.

View solution in original post


All Replies
Super User
Posts: 19,789

Re: macro variable in set statements based on today()

Posted in reply to Steelers_In_DC

Have you tried the short cut methods instead of listing all (colon and/or - between first and last datasets)?

Or extracting all the names from SASHELP.VTABLE?

 

 

Super User
Posts: 19,789

Re: macro variable in set statements based on today()

Posted in reply to Steelers_In_DC

data want;

set consumer2016: indsname=source;

dset_month = source;

run;

Valued Guide
Posts: 860

Re: macro variable in set statements based on today()

I don't know what you mean by 'between'.  I've never seen that.  I'm not familiar wiith indsname either, when I run that I get:

 

does not contain any members

Super User
Posts: 19,789

Re: macro variable in set statements based on today()

Posted in reply to Steelers_In_DC

Steelers_In_DC wrote:

I don't know what you mean by 'between'.  I've never seen that.  I'm not familiar wiith indsname either, when I run that I get:

 

does not contain any members


INDSNAME is an option that creates a variable that can store the name of the dataset a record comes from, when appending multiple datasets together. 

 

Post your log. As long as you're on SAS 9.3+ this should work, if you're on 9.2 then it won't.

Solution
‎12-22-2016 04:59 PM
Super User
Posts: 11,343

Re: macro variable in set statements based on today()

Posted in reply to Steelers_In_DC

Try:

%macro dates;
%do date = 201511 %to 201611;
fdr.consumer&dates
%end;
%mend;

data consumer_agg;

set
%dates
;
end;

3 problems: 1) you never called the macro

 

2) by putting ; after the fdr.consumer&dates value you would have terminated the set statement with the first name.

3) The data step ended on encountering the %macro statement as that is one of the boundaries between procedures. I suspect you got some odd message in the log about a data set with 0 variables.

 

Use

Options mprint symbolgen;

to see the code that is generated by your actual macro code.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 274 views
  • 1 like
  • 3 in conversation