BookmarkSubscribeRSS Feed
eemrun
Obsidian | Level 7

I am trying to do a macro loop for merging datasets with non-sequential names. For example: cust_012011, cust_022011, cust042011.

 

Unsure as to how I can get the macro loop to work. I have the following at the moment but this stops when there is a no dataset for a particular month:

 

%macro combine;
	%local tempTable;
	%do year = &startyr. %to &endyr.;
		%do month = 1 %to 12;
			%let tempTable = cust_data_&year%sysfunc(putn(&month, z2.));

			%if %sysfunc(exist(&temptable)) %then %do;
				Data want_temp_&month.;
					set &tempTable;
					keep cust_no data1;
					rename data1= data1&month.;
				run;	
			%end;
		%end;
	%end;

	data dataset;
	set
		%do i = 1 %to 12;
		want_temp_&i.
		%end;
	;
	by cust_no;
	run;

%mend ombine;
%combine;
5 REPLIES 5
SASKiwi
PROC Star

The best way to do this would be to build a list in a macro variable based on only datasets that exist using a DICTIONARY table:

 

proc sql noprint;
select memname
into  :data_list separated by ' '
from dictionary.members
where libname = "WORK" and memtype = "DATA"
and substr(memname, 1, 4) = 'CUST' 
;
quit;

%put &data_list;
eemrun
Obsidian | Level 7
thanks for this! But still unclear as to how to implement it.
Reeza
Super User

Are you positive you need a macro here?

It looks like an attempt at a proc transpose in a really long way. 

 

Does this get you part of the way there?

 

data _test;
set cust_data_: indsname=source;;
monthV = source;

run;

If you have any control over your naming convention, SAS works with prefixes so changing the names to be cust_data_YEAR_MONTH would make this a lot more efficient. 

 


@eemrun wrote:

I am trying to do a macro loop for merging datasets with non-sequential names. For example: cust_012011, cust_022011, cust042011.

 

Unsure as to how I can get the macro loop to work. I have the following at the moment but this stops when there is a no dataset for a particular month:

 

%macro combine;
	%local tempTable;
	%do year = &startyr. %to &endyr.;
		%do month = 1 %to 12;
			%let tempTable = cust_data_&year%sysfunc(putn(&month, z2.));

			%if %sysfunc(exist(&temptable)) %then %do;
				Data want_temp_&month.;
					set &tempTable;
					keep cust_no data1;
					rename data1= data1&month.;
				run;	
			%end;
		%end;
	%end;

	data dataset;
	set
		%do i = 1 %to 12;
		want_temp_&i.
		%end;
	;
	by cust_no;
	run;

%mend ombine;
%combine;

 

DrAbhijeetSafai
Pyrite | Level 9

As per this documentation, it seems that it is not possible in SAS to 'Loop through a nonsequential list of values with a macro DO loop'

 

Thank you.

 

- Dr. Abhijeet Safai

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real
Patrick
Opal | Level 21

@DrAbhijeetSafai wrote:

As per this documentation, it seems that it is not possible in SAS to 'Loop through a nonsequential list of values with a macro DO loop'

 

Thank you.

 

- Dr. Abhijeet Safai


Not sure why you're answering on a question from 2018. 

If you read the note you cite you will also see that it proposes alternative syntax to achieve logically the same - a loop using a distinct list of values. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

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.

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
  • 5 replies
  • 2016 views
  • 5 likes
  • 5 in conversation