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

TOPIC: Change dataset with loop count into append statement

 

I have a macro that will loop and create a new dataset with a counter behind.

 

Code like this:

PROC IMPORT OUT=WORK.out&i  DATAFILE= "&dir/&name" 			
		/*excelout*/			
            DBMS=csv REPLACE;					
			delimiter='09'x;		
			getnames=no;		
		RUN;		

data test&i (drop=			
		%do k=1 %to &cnt;			
		&&col&k..			
		%end;			
		);	
			
		length station $10 voltage $10 year 8 month $20 transformer $10			
		Day $20 Date Time MW_Imp MW_Exp MVAR_Imp MVAR_Exp MVA Power_Factor 8;			
		format Time hhmm.;			
		set out&i. end=last;	

 

Currently the script will generate about 4 data sets if i have 4 external files by PROC IMPORT.

What i want is to eliminate the creation of multiple datasets but just append them into the master file. Is there a way to do so?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

You can, but you still need both the PROC IMPORT and the DATA step first.  For example, in PROC IMPORT, re-use the same data set:

 

proc import out=temp ......

 

Then in the DATA step, modify it:

 

data temp (drop= .... same as before ...,.);

set temp;

 ..... same as before .....;

run;

 

Then add it to a master data set:

 

proc append data=temp base=master;

run;

 

This change relies on the DATA step logic accurately guaranteeing the same structure for all the data sets.

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

Yes, if you read your data as text files. The infile statement can accept multiple file names. The files are simply read in sequence. Check the documentation for the infile statement.

PG
Astounding
PROC Star

You can, but you still need both the PROC IMPORT and the DATA step first.  For example, in PROC IMPORT, re-use the same data set:

 

proc import out=temp ......

 

Then in the DATA step, modify it:

 

data temp (drop= .... same as before ...,.);

set temp;

 ..... same as before .....;

run;

 

Then add it to a master data set:

 

proc append data=temp base=master;

run;

 

This change relies on the DATA step logic accurately guaranteeing the same structure for all the data sets.

Reeza
Super User

If your files are csv with the same structure you can read it in a single step and avoid a macro which is the better approach. PROC IMPORT may truncate records so I wouldn't recommend it either in a situation like this.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 768 views
  • 0 likes
  • 4 in conversation