Help using Base SAS procedures

Import modify export and set of files iteratively.

Reply
Occasional Contributor
Posts: 6

Import modify export and set of files iteratively.

Hi All, I am new to SAS and am stuck at a point and would appreciate help.

I need to import 20 files(path and name stored in a dataset), change the values in a particular column and then export them each with a specified name.I am not able to do this. Please help me out here.

Respected Advisor
Posts: 3,799

Re: Import modify export and set of files iteratively.

Please specify the FILE TYPE.  If they are text you may not need the IMPORT/EXPORT bits.

Occasional Contributor
Posts: 6

Re: Import modify export and set of files iteratively.

Posted in reply to data_null__

.csv it is

Respected Advisor
Posts: 3,799

Re: Import modify export and set of files iteratively.

How do you identify the fields and values that need to be changed?  If you summpy example data you will getter feeback but the basics are this.

data _null_;

     set driver;

     create new ouput file name from file name in driver.

     infile dummy1 csv filevar=file-name-variable-from-driver end=eof;

     file dummy2 csv filevar=new-file-name;

     do while(not eof);

          input.....;

         something

         put ....;

         end;

   run;

Occasional Contributor
Posts: 6

Re: Import modify export and set of files iteratively.

Posted in reply to data_null__

Hi,

I created a dataset with a single column(to_be_imported) which contains the path of those 20 .csv to be imported.

Now, I am stuck at this step:

for(i=1 to 20)

1. Import ith csv into a dataset say work. Edit a column. Export it back with the name xyz||i.csv into a specified folder.

2.i=i+i+1;

Respected Advisor
Posts: 3,799

Re: Import modify export and set of files iteratively.

show your work.

Occasional Contributor
Posts: 6

Re: Import modify export and set of files iteratively.

Posted in reply to data_null__

Hi,

Thanks for being patient. I succeeded with the part of importing them all one by one but I have a new trouble now. In the highlighted step, VAR2 column is getting

replaced but its truncated. Eg: If value_after_editing = HANDWASH, only HANDWA is coming in VAR2 column. It is truncating after 6/7 chars. I tried INFORMAT but then column becomes empty. Help please!

%MACRO edit_file(dataset, value_after_editing);

data &dataset;

set &dataset;

VAR2= "& value_after_editing";

run;

%mend edit_file;

%MACRO SCANLOOP(SCANFILE,FIELD1,FIELD2,FIELD3);

/* First obtain the number of */

/* records in DATALOG */

DATA _NULL_;

IF 0 THEN SET &SCANFILE NOBS=X;

CALL SYMPUT('RECCOUNT',X);

STOP;

RUN;

/* loop from one to number of */

/* records */

%DO I=1 %TO &RECCOUNT;

/* Advance to the Ith record */

DATA _NULL_;

SET &SCANFILE (FIRSTOBS=&I);

/* store the variables */

/* of interest in */

/* macro variables */

CALL SYMPUT('csv_to_imported',&FIELD1);

CALL SYMPUT('dataset_name_After_importing',&FIELD2);

CALL SYMPUT('Edited_value_in_column_2',&FIELD3);

STOP;

RUN;

/* now perform the tasks that */

/* wish repeated for each */

/* observation */

%import_file(&csv_to_imported, &dataset_name_After_importing);

%edit_file(&dataset_name_After_importing,&Edited_value_in_column_2);

%END;

%MEND SCANLOOP;

/* Call SCANLOOP */

%SCANLOOP(DATALOG,FILENM,FIELD2,FIELD3);

RUN;

Super User
Posts: 11,343

Re: Import modify export and set of files iteratively.

The truncation in your example likely means the the original dataset when created is creating var2 with a length of $ 6. You may need to consider how you are creating the datasets to ensure your variables are long enough to contain the desired change values.

Occasional Contributor
Posts: 6

Re: Import modify export and set of files iteratively.

No. I checked in the original dataset. It has complete names. It's only when I pass that in a macro, it gets truncated. What to do?

Occasional Contributor
Posts: 6

Re: Import modify export and set of files iteratively.

But I will recheck and post.Also wanted to ask if it is possible to replace an excel with an exported excel of different name?

Ask a Question
Discussion stats
  • 9 replies
  • 357 views
  • 3 likes
  • 3 in conversation