LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (September 2008, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 5 Sep 2008 09:11:38 -0500
Reply-To:     Mary <mlhoward@avalon.net>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Mary <mlhoward@AVALON.NET>
Subject:      Re: Macro conversion of files
Comments: To: cherish k <hawks_cherish@YAHOO.CO.IN>
Content-Type: text/plain; format=flowed; charset="UTF-8"; reply-type=original

Cherish,

It looks like these all have the same variables, and if so, why do you need to put them all in 1000 different data sets? Here is code that I use to read in 200 tab delimited files all in the same directory that all have the same format and I put them all in the same data set; note I'm picking up the dataset name from the dirread and having that be a variable in my final file.

%macro read; data set1_all; stop; run;

filename exdir "C:\Work_Activities\Production_Source\Raw_Data\study_files"; %let dirid = %sysfunc(DOPEN(exdir)); %let dircnt = %sysfunc(DNUM(&dirid)); %do i = 1 %to &dircnt; %let dirread = %sysfunc(DREAD(&dirid,&i)); %put dirread = &dirread;

data temp; informat dummy 1.; stop; run;

data WORK.TEMP; ; infile "C:\Work_Activities\Production_Source\Raw_Data\study_files\&dirread" delimiter='09'x MISSOVER DSD lrecl=32767 firstobs=2; informat STUDY_NBR $50. /* rest of variables */ ; format STUDY_NBR $50. /* rest of variables */ ; input STUDY_NBR $ /* rest of variables */ ; run;

proc sql noprint; select count(*) as count into :count from temp; quit;

%if &count > 0 %then %do; %Let datasetname = %scan(&dirread,1,.); data temp; informat datasetname $50.; set temp; datasetname="&datasetname"; run; data set1_all; set set1_all temp; run; %end; %end; %let rc = %sysfunc(DCLOSE(&dirid)); %put rc = &rc; %mend read;

%read;

-Mary

----- Original Message ----- From: cherish k To: SAS-L@LISTSERV.UGA.EDU Sent: Friday, September 05, 2008 3:29 AM Subject: Macro conversion of files

Hi All,

I have approximately 1000 files in txt format separated by comma. I need to convert them in SAS datasets with a single conversion code. File formats are same for all the files.

Only problem being that the file names are in this format: file_000001

so if the file number is 95 then the file name is: file_000095

I used the following code

%macro conv(j,k);

data File_&k.; infile "C:\Documents and Settings\cherish\My Documents\Cherish\file_&j..txt" delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=2 ; informat VAR1 best32. ; format VAR1 best12. ; input VAR1 ; FILE_NO = &k; run;

%mend conv;

%macro conv_call;

%do i = 1 %to 1000;

%if %eval(&i) < 10 %then %conv(00000&i.,&i.);%else %if %eval(&i) < 100 %then %conv(0000&i.,&i.);%else %if %eval(&i) < 1000 %then %conv(000&i.,&i.);

%end;

%mend conv_call;

%conv_call;

Is there a better way of doing this? i.e. instead of writing so many %if %else statements?

Cherish

--------------------------------- Unlimited freedom, unlimited storage. Get it now


Back to: Top of message | Previous page | Main SAS-L page