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

 

 


****************************************Example*****************************************************

%macro import_loop; %let outobs=max; %do i1=0 %to 24; %let i = %sysfunc( putn(&i1,z2.)); proc import out = testv1_&i datafile = '/TESTING/DVR/DVR_V1_PROD_01182017.xls' dbms = xls ; sheet ="M&i._ACCT_BAL_AM"; getnames = yes; run; proc sql outobs=&outobs; create table M&i._ACCT_BALANCE_AM as select "M_ACCT_BAL_AM_&i." as M_ACCT_BAL_AM_&i. ,a.* FROM testv1_&i a ; %end; %mend import_loop; %import_loop;

 Hi Team,

Somone of you have you ever used **Union with Proc SQL** using macrovariables??

With the macro shown  above I created 25 tables.
I want to create in the last step only one table which contains 25 tables
previously created using **Union of Proc SQL with macrovariables**
..

How can I do it using Macrovariables in a simple way similar that I created the 25 tables??

Thanks and Regards,

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Use a dataset list within a set statement to read all datasets and option indsname to get their names:

 

%macro import_loop;
%do i1=0  %to 24;
	%let i = %sysfunc( putn(&i1,z2.));
	proc import  
							out  		= testv1_&i 
							datafile 	= '/TESTING/DVR/DVR_V1_PROD_01182017.xls'
							dbms 		= xls ;
							sheet		="M&i._ACCT_BAL_AM";
							getnames 	= yes;
	run;
%end;

data allMyData;
length fromSheet $32;
set testv1_: indsname=name;
fromSheet = cats("M", scan(name, 3, "._"), "_ACCT_BAL_AM");
run;

%mend import_loop;

%import_loop;
PG

View solution in original post

7 REPLIES 7
DanielSantos
Barite | Level 11

Hi.

 

Why not using PROC APPEND?

 

%macro import_loop;

proc datasets lib=WORK nolist;
     delete M_ALL;
quit; * delete first;

%let outobs=max;
%do i1=0  %to 24;
%let i = %sysfunc( putn(&i1,z2.));
proc import 			out  		= testv1_&i 
    					datafile 	= '/TESTING/DVR/DVR_V1_PROD_01182017.xls'
    					dbms 		= xls ;
    					sheet		="M&i._ACCT_BAL_AM";
    					getnames 	= yes;
run;
proc sql outobs=&outobs;
create table M&i._ACCT_BALANCE_AM
as
select  "M_ACCT_BAL_AM_&i." as  M_ACCT_BAL_AM_&i.
	   ,a.*
FROM testv1_&i  a
;

proc append base=M_ALL data=M&i._ACCT_BALANCE_AM force;
run; * append;

%end;
%mend import_loop;
%import_loop;

 

 

Daniel Santos @ www.cgd.pt

 

Jcorti
Obsidian | Level 7

Thanks a lot man for your replay

PGStats
Opal | Level 21

Use a dataset list within a set statement to read all datasets and option indsname to get their names:

 

%macro import_loop;
%do i1=0  %to 24;
	%let i = %sysfunc( putn(&i1,z2.));
	proc import  
							out  		= testv1_&i 
							datafile 	= '/TESTING/DVR/DVR_V1_PROD_01182017.xls'
							dbms 		= xls ;
							sheet		="M&i._ACCT_BAL_AM";
							getnames 	= yes;
	run;
%end;

data allMyData;
length fromSheet $32;
set testv1_: indsname=name;
fromSheet = cats("M", scan(name, 3, "._"), "_ACCT_BAL_AM");
run;

%mend import_loop;

%import_loop;
PG
Jcorti
Obsidian | Level 7

Thanks Man!! 🙂 Actually was not needed proc sql I removed that part;

 

Could you please explain me what those statements do?

 

 

set testv1_: indsname=name;
fromSheet = cats("M", scan(name, 3, "._"), "_ACCT_BAL_AM");

 

 

My guessing is the new variable **fromsheet" is selecting all variables that contains **_ACCT_BAL_AM**

When I am creating  **fromSheet variable** can I include in addition of _ACCT_BAL_AM another variables??

I am asking because in my excel file DVR_V1_PROD_01182017 has more variables like  **M&i._ACT_PYMT_AMT**

 

Thanks a lot,

 

 

 

 

 

 

 

PGStats
Opal | Level 21

set testv1_: reads all datasets in the WORK library with names starting with testv1_ 

indsname=name creates a variable named name that will be assigned the name of the dataset being read (eg "WORK.TESTV1_04")

scan(name, 3, "._") extracts the number from the dataset name (eg "04")

cats("M", scan(name, 3, "._"), "_ACCT_BAL_AM") rebuilds the Excel sheet name (eg "M04_ACCT_BAL_AM")

 

PG
Jcorti
Obsidian | Level 7

Thanks a lot for easy explanation!!

Jcorti
Obsidian | Level 7

 

Hi again,

 

Do you know if I can reference more variables here??

 

Asking because I need to cosolidate  into:

 

 

data allMyData;
length fromSheet $32;
set testv1_: indsname=name;
fromSheet = cats("M", scan(name, 3, "._"), "_ACCT_BAL_AM");
run;

 

more macrovariables highlighted in black below:

 


%do i1=0 %to 24; %let i = %sysfunc(putn(&i1,z2.)); %let cons=consolidated; %let var1=M&i._ACCT_BALANCE_AM;

 

 

%let var2=M&i._ACCT_CREDIT_LIMIT_AM;
%let var3=M&i._ACTUAL_PYMT_AM;

%let var4=M&i._ACCT_PAYMENT_AM; 

 

Thanks a lot in advance,

 

 

JC

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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