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

Hi,

I need to display the list of csv files in a directory by invoking a SAS Window, where I should be able to select a desired file . I have a macro code to display the csv files from a directory. But Iam stuck at displaying them on a Window. I am new to SAS but I developed a logic something like this, but Iam not sure whether %window has do loops, so resulting in an error. Please help.

 

 %Window displayfiles
 ICOLUMN= 15 IROW= 10
 COLUMNS= 80 ROWS= 20
 #3 @15 "List of &csvfile "
%do i=1 %to &count;
 #(&i+5) file.&i @15 " &name"

ATTR=UNDERLINE REQUIRED=YES

 #(&i+6) @15 "Please enter Y for the selected file" @40 selectfile 1
 #(&i+7) @15 "Press Enter to continue..."
 ;

 

/* Macro for selecting the CSV files */

 

%macro drive(dir,ext);      

%local filrf rc did memcnt name i ;    
 **%let count=0;                                                                                                                     
  /* Assigns a fileref to the directory and opens the directory */                                                         
%let rc=%sysfunc(filename(filrf,&dir));                                                                                             
%let did=%sysfunc(dopen(&filrf));                                                                                                   
                                                                                                                                      
/* Make sure directory can be open */                                                                                               
%if &did eq 0 %then %do;                                                                                                            
%put Directory &dir cannot be open or does not exist;                                                                              
%return;                                                                                                                           
%end;                                                                                                                               
                                                                                                                                      
   /* Loops through entire directory */                                                                                               
%do i = 1 %to %sysfunc(dnum(&did));                                                                                                
                                                                                                                                      
     /* Retrieve name of each file */                                                                                                 
%let name=%qsysfunc(dread(&did,&i));                                                                                             
                                                                                                                                      
     /* Checks to see if the extension matches the parameter value */                                                                 
     /* If condition is true print the full name to the log        */                                                                 
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;                                                                     
 
%let count=%eval(&count+1);
%put &name;  
%end;                                                                                                                           
                                                                                                                      
%end;                                                                                                                              
                                                                                                                         
  /* Closes the directory and clear the fileref */                                                                                    
%let rc=%sysfunc(dclose(&did));                                                                                                     
%let rc=%sysfunc(filename(filrf));                                                                                                  
                                                                                                                                      
%mend drive;                                                                                                                          
                                                                                                                                      
/* First parameter is the directory of where your files are stored. */                                                                
/* Second parameter is the extension you are looking for.           */                                                                
                                                             
%drive(c:\mis6334,&filetype)

1 ACCEPTED SOLUTION

Accepted Solutions
UPEN
Obsidian | Level 7
%unquote(# &val @15 "&name" attr= underline;)

I removed the braces for &val

 

This worked, thanks for your time and help!

View solution in original post

10 REPLIES 10
Reeza
Super User

You can do this type of programming in SAS but it's better to use a different language IMO. Do you have SASEG or SAS Studio?

UPEN
Obsidian | Level 7

I am using Base SAS 9.4, I have to use only %window, because this piece of code is just a starting step to the next part of the code. Besides I am not familiar with IMO language

art297
Opal | Level 21

Take a look at the example on the last page of: http://www.lexjansen.com/wuss/2003/SASSolutions/c-using_window.pdf

 

You could always use that method to list your files, but just have one entry field, e.g.

Enter the number of the file you want to process

 

Art, CEO, AnalystFinder.com

 

UPEN
Obsidian | Level 7

Based on this paper, I modified my code as below, but this gives me error.

 

 %macro drive(dir,ext);                                                                                                                  
%local filrf rc did memcnt name i ;      
 **%let count=0;                                                                                                                       
  /* Assigns a fileref to the directory and opens the directory */                                                           
%let rc=%sysfunc(filename(filrf,&dir));                                                                                               
%let did=%sysfunc(dopen(&filrf));                                                                                                     
                                                                                                                                        
/* Make sure directory can be open */                                                                                                 
%if &did eq 0 %then %do;                                                                                                              
%put Directory &dir cannot be open or does not exist;                                                                                
%return;                                                                                                                             
%end;                                                                                                                                 
                                                                                                                                        
   /* Loops through entire directory */                                                                                                 
%do i = 1 %to %sysfunc(dnum(&did));                                                                                                  
                                                                                                                                        
     /* Retrieve name of each file */                                                                                                   
%let name=%qsysfunc(dread(&did,&i));                                                                                               
                                                                                                                                        
     /* Checks to see if the extension matches the parameter value */                                                                   
     /* If condition is true print the full name to the log        */                                                                   
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;                                                                       

%put &name;   

#%eval(&i + 2) @15 "&name" attr = underline  

%end;                                                                                                                             
                                                                                                                        
%end;                                                                                                                                
                                                                                                                             
  /* Closes the directory and clear the fileref */                                                                                      
%let rc=%sysfunc(dclose(&did));                                                                                                       
%let rc=%sysfunc(filename(filrf));                                                                                                    
                                                                                                                                        
%mend drive;                                                                                                                            
                                                                                                                                        


%WINDOW showfiles
 ICOLUMN= 15 IROW= 10
 COLUMNS= 80 ROWS= 20
/* First parameter is the directory of where your files are stored. */                                                                  
/* Second parameter is the extension you are looking for.           */                                                                  
%drive(c:\mis6334,csv)  
%display showfiles;

ballardw
Super User

instead of

#%eval(&i + 2) @15 "&name" attr = underline  

 

Try

%let val = %eval(&i+2);

#&val @15 "&name" attr= underline

 

When you get an error post the log with the error message into a code box opened with the forun {i} menu icon.

The errors often have indicators as to where the error occurred but the main message windows will reformat code removing that.

If the code is in a macro then use OPTIONS MPRINT SYMBOLGEN MLOGIC; before running the code so the error message will appear near the actual error and not after the macro.

 

It is always a good idea to post code of more than a couple lines in a code box.

UPEN
Obsidian | Level 7
%macro drive(dir,ext);                                                                                                                  
%local filrf rc did memcnt name i ;      
 **%let count=0;                                                                                                                       
  /* Assigns a fileref to the directory and opens the directory */                                                           
%let rc=%sysfunc(filename(filrf,&dir));                                                                                               
%let did=%sysfunc(dopen(&filrf));                                                                                                     
                                                                                                                                        
/* Make sure directory can be open */                                                                                                 
%if &did eq 0 %then %do;                                                                                                              
%put Directory &dir cannot be open or does not exist;                                                                                
%return;                                                                                                                             
%end;                                                                                                                                 
                                                                                                                                        
   /* Loops through entire directory */                                                                                                 
%do i = 1 %to %sysfunc(dnum(&did));                                                                                                  
                                                                                                                                        
     /* Retrieve name of each file */                                                                                                   
%let name=%qsysfunc(dread(&did,&i));                                                                                               
                                                                                                                                        
     /* Checks to see if the extension matches the parameter value */                                                                   
     /* If condition is true print the full name to the log        */                                                                   
%if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;                                                                       

%put &name;   

%let val = %eval(&i+2);

# (&val) @15 "&name" attr= underline;

%end;                                                                                                                             
                                                                                                                        
%end;                                                                                                                                
                                                                                                                             
  /* Closes the directory and clear the fileref */                                                                                      
%let rc=%sysfunc(dclose(&did));                                                                                                       
%let rc=%sysfunc(filename(filrf));                                                                                                    
                                                                                                                                        
%mend drive;                                                                                                                            
                                                                                                                                        
%drive(c:\testing,csv);  

options mlogic symbolgen mprint;
%WINDOW showfiles
 ICOLUMN= 15 IROW= 10
 COLUMNS= 80 ROWS= 20
/* First parameter is the directory of where your files are stored. */                                                                  
/* Second parameter is the extension you are looking for.           */                                                                  
%drive(c:\testing,csv);  
%display showfiles;
1    %macro drive(dir,ext);
2    %local filrf rc did memcnt name i ;
3     **%let count=0;
4      /* Assigns a fileref to the directory and opens the directory */
5    %let rc=%sysfunc(filename(filrf,&dir));
6    %let did=%sysfunc(dopen(&filrf));
7
8    /* Make sure directory can be open */
9    %if &did eq 0 %then %do;
10   %put Directory &dir cannot be open or does not exist;
11   %return;
12   %end;
13
14      /* Loops through entire directory */
15   %do i = 1 %to %sysfunc(dnum(&did));
16
17        /* Retrieve name of each file */
18   %let name=%qsysfunc(dread(&did,&i));
19
20        /* Checks to see if the extension matches the parameter value */
21        /* If condition is true print the full name to the log        */
22   %if %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) %then %do;
23
24   %put &name;
25
26   %let val = %eval(&i+2);
27
28   # (&val) @15 "&name" attr= underline;
29
30   %end;
31
32   %end;
33
34     /* Closes the directory and clear the fileref */
35   %let rc=%sysfunc(dclose(&did));
36   %let rc=%sysfunc(filename(filrf));
37
38   %mend drive;
39
40   %drive(c:\testing,csv);
UTDtrainPrepped.csv
41
42   options mlogic symbolgen mprint;
43   %WINDOW showfiles
44    ICOLUMN= 15 IROW= 10
45    COLUMNS= 80 ROWS= 20
46   /* First parameter is the directory of where your files are stored. */
47   /* Second parameter is the extension you are looking for.           */
48   %drive(c:\testing,csv);
MLOGIC(DRIVE):  Beginning execution.
MLOGIC(DRIVE):  Parameter DIR has value c:\testing
MLOGIC(DRIVE):  Parameter EXT has value csv
MLOGIC(DRIVE):  %LOCAL  FILRF RC DID MEMCNT NAME I
MLOGIC(DRIVE):  %LET (variable name is COUNT)
MLOGIC(DRIVE):  %LET (variable name is RC)
SYMBOLGEN:  Macro variable DIR resolves to c:\testing
MLOGIC(DRIVE):  %LET (variable name is DID)
SYMBOLGEN:  Macro variable FILRF resolves to #LN00011
SYMBOLGEN:  Macro variable DID resolves to 1
MLOGIC(DRIVE):  %IF condition &did eq 0 is FALSE
SYMBOLGEN:  Macro variable DID resolves to 1
MLOGIC(DRIVE):  %DO loop beginning; index variable I; start value is 1; stop value is 3; by value is
      1.
MLOGIC(DRIVE):  %LET (variable name is NAME)
SYMBOLGEN:  Macro variable DID resolves to 1
SYMBOLGEN:  Macro variable I resolves to 1
SYMBOLGEN:  Macro variable NAME resolves to Q00066.xlsx
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted
            for printing.
SYMBOLGEN:  Macro variable EXT resolves to csv
MLOGIC(DRIVE):  %IF condition %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) is FALSE
MLOGIC(DRIVE):  %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(DRIVE):  %LET (variable name is NAME)
SYMBOLGEN:  Macro variable DID resolves to 1
SYMBOLGEN:  Macro variable I resolves to 2
SYMBOLGEN:  Macro variable NAME resolves to Q00080.xlsx
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted
            for printing.
SYMBOLGEN:  Macro variable EXT resolves to csv
MLOGIC(DRIVE):  %IF condition %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) is FALSE
MLOGIC(DRIVE):  %DO loop index variable I is now 3; loop will iterate again.
MLOGIC(DRIVE):  %LET (variable name is NAME)
SYMBOLGEN:  Macro variable DID resolves to 1
SYMBOLGEN:  Macro variable I resolves to 3
SYMBOLGEN:  Macro variable NAME resolves to UTDtrainPrepped.csv
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted
            for printing.
SYMBOLGEN:  Macro variable EXT resolves to csv
MLOGIC(DRIVE):  %IF condition %qupcase(%qscan(&name,-1,.)) = %upcase(&ext) is TRUE
MLOGIC(DRIVE):  %PUT &name
SYMBOLGEN:  Macro variable NAME resolves to UTDtrainPrepped.csv
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted
            for printing.
UTDtrainPrepped.csv
MLOGIC(DRIVE):  %LET (variable name is VAL)
SYMBOLGEN:  Macro variable I resolves to 3
NOTE 137-205: Line generated by the invoked macro "DRIVE".
1       **
        --
        22
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, #, +, /, //, @,
              COLOR, COLOUR, COLUMNS, GROUP, HELP, ICOLUMN, IROW, KEY, KEYS, MENU, MENUS, ROWS.

SYMBOLGEN:  Macro variable VAL resolves to 5
SYMBOLGEN:  Macro variable NAME resolves to UTDtrainPrepped.csv
SYMBOLGEN:  Some characters in the above value which were subject to macro quoting have been unquoted
            for printing.
NOTE: Line generated by the invoked macro "DRIVE".
1       **
        --
        200
ERROR 200-322: The symbol is not recognized and will be ignored.

ERROR: %WINDOW statement not processed due to errors.
MLOGIC(DRIVE):  %DO loop index variable I is now 4; loop will not iterate again.
MLOGIC(DRIVE):  %LET (variable name is RC)
SYMBOLGEN:  Macro variable DID resolves to 1
MLOGIC(DRIVE):  %LET (variable name is RC)
MLOGIC(DRIVE):  Ending execution.
49   %display showfiles;
ERROR: Unable to open window macro file SHOWFILES.
art297
Opal | Level 21

Unfortunately, I can't test it on SAS UE, but does it work if you use:

#&val @15 "&name" attr= underline;

rather than:

# (&val) @15 "&name" attr= underline;

 Art, CEO, AnalystFinder.com

 

UPEN
Obsidian | Level 7

Nope. It didnt work , I tried it in the first place.

FriedEgg
SAS Employee

Remove the line:

 

 **%let count=0;  

 

Or change it to a global comment.

 

I would also %unquote your driver text being passed back to the %window

 

i.e.

 

%unquote(# (&val) @15 "&name" attr= underline;)
UPEN
Obsidian | Level 7
%unquote(# &val @15 "&name" attr= underline;)

I removed the braces for &val

 

This worked, thanks for your time and help!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 10 replies
  • 1262 views
  • 1 like
  • 5 in conversation