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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

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.

Andysas9
Calcite | Level 5
Below is the code I used for each text file
data A_Sample;
infile 'C:\data\datasets\A_Sample.txt';
input
ID 1-16
FIRST_NAME $17-27
LAST_NAME $28 - 47
Phone_no 48 - 58
Activity_flag $59 - 59
;
Run;

Proc Sql;
create tabe A_Sample_test as
select * from A_Sample where Activity_flag = 'Y';
Quit;
Kurt_Bremser
Super User

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;

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 3 replies
  • 695 views
  • 1 like
  • 2 in conversation