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

I've Multiple text file in one location, I'm able to import them into one.

 

But Few files have 10 Variable/ few of them have 12 Variables/ Few of them are having 4 Variables.

 

The Variable which I wanted in final dataset present in all of them.

 

all of the text files have header and I want two of them.

 

Can I put those column name somewhere in below code ??

 

proc import datafile="Dir_Name/*.txt" out=work.Metadata dbms=dlm replace;
delimiter='09'x;
getnames=yes;
guessingrows=max;
quit;

1 ACCEPTED SOLUTION

Accepted Solutions
atul_desh
Quartz | Level 8
Its done now.. thanks..

I created temperory dataset from all files and then set in the final dataset (keeping only variable what i want).


%let i=1;
%do %while (%scan(&fn,&i,' ') ne );
proc import datafile="&D_IN_reason/%scan(&fn,&i,' ')" out=work.chk_&i dbms=dlm replace;
delimiter='09'x;
getnames=yes;
guessingrows=max;
run;
%put %scan(&fn,&i,' ');
%let i=%eval(&i+1);
%end;
run;
%mend cr_agg_list;

%cr_agg_list;

options symbolgen mlogic mprint;

data fnl_chk;
attrib filename format=$200.;
set chk_1-chk_&n_obs (keep=Filename TotalRecords);
run;

View solution in original post

7 REPLIES 7
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Well, I would only use proc import to generate the datastep for you.  Once you have that code, update it by hand:

data want (keep=the_variable_you_want);
  infile "c:\temp\*.txt" dlm='09'x;
  input the_variable_you_want $ something_else ...;
run;

If the other files don't contain all the varibles then you might get problems, hard to say without seeing the actual data.  By similar are those columns completely missing?  If so you would need either a program for each import, or you would read the whole of each file line by line and parse out the resulting strings.

atul_desh
Quartz | Level 8
it is not working..

data want (keep=FileName TotalRecords);
infile "Path" dsd dlm='09'x;
input FileName : $50. TotalRecords : $50. ;
run;

It is taking variable names according to occurence in the file.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You haven't said what "is not working".  It will read in the data per the way you have coded it, if its not reading in the data correctly you need to code it per the data??

Tom
Super User Tom
Super User

If you only want to keep some of the variables you can add the KEEP= dataset option to the output dataset name.

proc import datafile="Dir_Name/*.txt" dbms=dlm
  out=Metadata(keep=var1 var2 var3) replace
;
  delimiter='09'x;
  getnames=yes;
  guessingrows=max;
quit;

Note that it is probably better to NOT use PROC IMPORT to guess at what is in your data files.  Especially if you are going to combine multiple files.  The only metadata on your variables that a delimited file contains are the column headers. Otherwise PROC IMPORT has to guess at how to define your variables.  So if one file only has short names then it make make the name field too short.  Or worse it might define as a number a field that should be character.

atul_desh
Quartz | Level 8
Problem is,

1st file is having 12 variable (Var1[Name] to Var12 [Country]) and let say 6th file is having 4 Variable (Var1[Name] to Var4[Country]).

when I'm reading all data Var12[Country] is coming blank for 6th File.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

As I mention above, you need to code for your data.  There is no magic wand to do your job.

atul_desh
Quartz | Level 8
Its done now.. thanks..

I created temperory dataset from all files and then set in the final dataset (keeping only variable what i want).


%let i=1;
%do %while (%scan(&fn,&i,' ') ne );
proc import datafile="&D_IN_reason/%scan(&fn,&i,' ')" out=work.chk_&i dbms=dlm replace;
delimiter='09'x;
getnames=yes;
guessingrows=max;
run;
%put %scan(&fn,&i,' ');
%let i=%eval(&i+1);
%end;
run;
%mend cr_agg_list;

%cr_agg_list;

options symbolgen mlogic mprint;

data fnl_chk;
attrib filename format=$200.;
set chk_1-chk_&n_obs (keep=Filename TotalRecords);
run;

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