I have multiple raw files without header . I want to automate the process in order to create multiples datasets and also once the dataset created need to automate the process when we use logic say **Actvity_flag ='Y'**
, I need those datasets to be created which has data in it. No need of datasets with Null Rows.
Below is the Sample Text file
A_Sample.txt
B_Sample.txt
C_Sample.txt
D_Sample.txt
Path: C:\Data\datasets*****.txt
Below are the Variables
Id 1-16
First_Name 17-27
Last-named 28-47
Phone_no 48-58
Activity_Flag 59-59
Data in text file look like as below
10001 George Michael 123456789 Y
10002 Henry Jha 987456123 Y
10003 Rob Camer 258963147 N
10004 Allan Cruze 369852147 Y
10005 Andy wilson 147258369 N
So you need to replace all occurrences of A_Sample with a macro variable, and then wrapthe code into a macro that accepts a parameter:
%macro import_text(fname);
data &fname.;
infile "C:\data\datasets\&fname..txt";
input
ID 1-16
FIRST_NAME $17-27
LAST_NAME $28 - 47
Phone_no 48 - 58
Activity_flag $59 - 59
;
Proc Sql;
create table &fname._test as
select * from &fname. where Activity_flag = 'Y';
Quit;
%if &sqlobs. = 0
%then %do;
proc delete data=
&fname.
&fname._test
;
run;
%end;
%mend;
%import_text(A_Sample)
You can now call this macro repeatedly from a dataset or from a text file with CALL EXECUTE:
data _null_;
set datasets;
call execute('%nrstr(%import_text('!!trim(fname)!!'))');
run;
Rule #1 of macro development: start with working non-macro code.
So please show us the code you use to create one text file from one dataset.
Correction: one dataset from one text file.
So you need to replace all occurrences of A_Sample with a macro variable, and then wrapthe code into a macro that accepts a parameter:
%macro import_text(fname);
data &fname.;
infile "C:\data\datasets\&fname..txt";
input
ID 1-16
FIRST_NAME $17-27
LAST_NAME $28 - 47
Phone_no 48 - 58
Activity_flag $59 - 59
;
Proc Sql;
create table &fname._test as
select * from &fname. where Activity_flag = 'Y';
Quit;
%if &sqlobs. = 0
%then %do;
proc delete data=
&fname.
&fname._test
;
run;
%end;
%mend;
%import_text(A_Sample)
You can now call this macro repeatedly from a dataset or from a text file with CALL EXECUTE:
data _null_;
set datasets;
call execute('%nrstr(%import_text('!!trim(fname)!!'))');
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.