Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating macro names from folder files names 1

Status
Not open for further replies.

bumed

Technical User
Aug 23, 2007
18
US
Hi all,
I'm kind of new to using macros, mostly us SAS for data analysis. I have setup a macro to do what I want but the problem is that it must be run on about 600 small files in a folder. There has got to be a better way then invoking a macro for each of the files by hand. Is there a way to look in the folder and pull the names that way. Thanks for the help and have a great holiday.
Steve
 
You have to be more specific, what are trying to do. Perhaps a seudo code example... There is a way of getting all the file names in a folder and creating macro names of them, but tell us more about what you need done and I can tailor the example to your specific problem.
Klaz
 
Well here is my macro, What I would like to do is take all the file in a folder and run them though this macro and the concatenate them all into a large file with all the data. But I don't want to have to invoke Dpull for each file by typing it out, there has to be a way of pulling the names from the file in the folder and using them for fname.

%macro Dpull (fname);
data A_&fname;
infile "\\Ad.bu.edu\dfs\bumc\BUSM\Anatomy and Neurobiology\Dept\LDCN\Autism\U-19 2002-2007\Wave3_PII\Production study\
Face Coding - Noldus\Noldus\The Observer\Workspaces\Projects\testing face coding2\Data Files cleaned\&fname..odf" firstobs= 13;
input time code $;
run;
Data B_&fname;
set A_&fname;
if code = "neutral" then emotion2=1;
if code = "anger" then emotion2=2;
if code = "happy" then emotion2=3;
if code = "surprise" then emotion2=4;
if code = "fear" then emotion2=5;
if code = "other" then emotion2=6;

if code = "flat" then expressiveness2=1;
if code = "mild" then expressiveness2=2;
if code = "moderate" then expressiveness2=3;
if code = "extreme" then expressiveness2=4;

if code = "fully" then naturaleness2=1;
if code = "slightly" then naturaleness2=2;
if code = "very" then naturaleness2=3;
if code = "unnatural" then naturaleness2=4;

if code = "on" then gaze2=1;
if code = "away" then gaze2=2;

data C_&fname;
set B_&fname;
emotion = lag3(emotion2);
expressiveness = lag2(expressiveness2);
naturaleness = lag(naturaleness2);
gaze = gaze2;

data D_&fname;
set C_&fname (keep = emotion expressiveness naturaleness gaze time);
if gaze = "." then delete;

%mend Dpull;

%Dpull(********);
 
As a side note while I have you I need to get a bit of info from data that is very messy. But the information I need always comes before .avi in the text file. I know the column pointer can read after a "character" but can it read before the "character".

Exp
C:\Documents and Settings\All Users\Noldus\The Observer\Media\7394.01\7394.01_3.avi
 
There is a way to do this automatically.

First, you need to read all the file names in that folder like this;

Code:
%MACRO CALLDPULL(FOLDER=c:\temp\example);


/*** THIS IS THE DOS DIRECTORY LIST COMMAND (IN UNIX ITS LS)***/
filename _in pipe "dir &folder  /b";

data filelist;
 infile _in;
 length fullfilename $500;
 *** THE AMP SYMBOL ALLOWS SAS TO IGNORE THE SPACES IN FILE NAMES ***;
 input fullfilename &;
run;
/** YOU CAN SORT IF YOU WANT TO **/
/*
proc sort
  data = filelist;
  by fullfilename;
run;
*/
/*** GENERATE YOUR MACRO VARIABLES WITH A LIMIT VALUE ***/
data _null_;
  set filelist end=last;
  call symput('fname'||trim(left(put(_n_,8.))),fullfilename);
  if last then
    call symput('limit',trim(left(put(_n_, 8.))) );
run;
/*** YOU SHOULD RELEASE THE FILENAME HANDLE FOR A CLEAN CODE ***/
filename _in;

/*** NOW CALL YOUR MACRO DPULL ***/
%for i=1 %to &limit;
  %Dpull(&&fname&i); 
%end;
%MEND CALLDPULL;

I hope this helps you. You may need to modify parts of this example for your specific project.

Have a happy holiday. :)
Klaz
 
I tried to modify the code but I'm not that experienced and could use some help. Do I just place this code after my code? Do I replace FOLDER= with the folder that contains the files? Do I have to change anything about "dir &folder /b"?
 
I'm getting an error at the end of the code.
There is no matching %DO statement for the %END. This statement will be ignored
 
Sorry, I noticed that 4 lines from the end of my code I used a FOR instead of a DO. Change the %FOR to %DO. As for where to put this code, try putting after your dpull macro. The path to the folder that you need should be inserted into the %CALLDPULL macro.

%CALLDPULL(FOLDER=c:\temp\example);


On another note I noticed that your logic states that if you find a code=flat you set a value of '1', yet if you find another code=fully you also set it to '1'. Is this what you want to do? You could write both of the '1' values in the same statement.

Klaz

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top