DATA Step, Macro, Functions and more

can I choose few variable name from multiple text file

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

can I choose few variable name from multiple text file

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;


Accepted Solutions
Solution
‎05-24-2017 05:30 AM
Contributor
Posts: 60

Re: can I choose few variable name from multiple text file

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


All Replies
Super User
Super User
Posts: 7,401

Re: can I choose few variable name from multiple text file

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.

Contributor
Posts: 60

Re: can I choose few variable name from multiple text file

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.
Super User
Super User
Posts: 7,401

Re: can I choose few variable name from multiple text file

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??

Super User
Super User
Posts: 6,500

Re: can I choose few variable name from multiple text file

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.

Contributor
Posts: 60

Re: can I choose few variable name from multiple text file

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.

Super User
Super User
Posts: 7,401

Re: can I choose few variable name from multiple text file

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

Solution
‎05-24-2017 05:30 AM
Contributor
Posts: 60

Re: can I choose few variable name from multiple text file

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;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 143 views
  • 0 likes
  • 3 in conversation