How to write batch file in SAS that automates the opening of files?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 75
Accepted Solution

How to write batch file in SAS that automates the opening of files?

I was assigned a task that I don’t know where to start. Here’s the context:

 

There’s a variable in the data, say VAR1, indicating the directory to a bunch of image files. So for observation 1, VAR1 may look like D:\Project\Data\Images\Image1.tiff and so on. Of course, those image files exist in the computer.

 

What I need to do is to figure out SAS program(s) and later run them automatically using batch file. When the batch file runs, it will, in some way, opens the image files one by one. By “one by one”, I mean it firsts open one image file and, upon closing that file, it opens the next image file until the end of the list.

 

Better yet, the batch file will make a copy of the original image files and put them in some folder (e.g., D:\Project\Data\Temp images) before opening them. That is to make sure original data is left untouched.

 

Do you know how I can write such a program in SAS? I was given the following SPSS file for reference, which does that job nicely as described. I don’t know enough SPSS to understand every detail how it works. The two variables dir5 and tiff5 specify the location of the image files, and variables SCQID and ohhscqid are just ID variables.

 

string out2 (a200).

 

compute out2=concat('copy "', ltrim(rtrim(dir5)),"\", tiff5, '"',' "c:\temp\temp.tiff"').

 

write outfile='E:\Data\Outcome.bat'/'@echo SCQ ID ' ohhscqid .

 

write outfile='E:\Data\Outcome.bat'/out2.

 

write outfile='E:\Data\Outcome.bat'/'@"C:\Program Files\Microsoft Office\Office14\OIS.exe" "c:\temp\temp.tiff"'.

 

execute.


Accepted Solutions
Solution
‎02-14-2016 08:01 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to write batch file in SAS that automates the opening of files?

 I am unable to check this at the moment, as in meeting, but if your dead set on going that route against all advice, then you can trim your code down (use functions cats/x/t and quote rather than lines of quotes):

data _null_;
  set have;
  file "E:\SAS codes and files\batchfile.bat";
  put cats('@echo SCQ ID ',ohhscqid);
  put cats('copy ',quote(cats(dir5,'\',tiff5')));
  put cats('@',quote('C:\Program Files\Microsoft Office\Office14\OIS.exe'));
  put quote('c:\temp\temp.tiff');
run;

 

 

 

 

 

View solution in original post


All Replies
Grand Advisor
Posts: 17,338

Re: How to write batch file in SAS that automates the opening of files?

What is the process on opening the files? Is SAS or another program doing something? 

 

This is could be done via SAS but honestly your better off doing it via an OS script, are you on Windows?

 

https://communities.sas.com/t5/SAS-Analytics-U/bd-p/sas_analytics_u

 

Frequent Contributor
Posts: 75

Re: How to write batch file in SAS that automates the opening of files?

I'm honestly not sure about the process, but my guess from the SPSS syntax is that, after the batch file is created, it has nothing to do with SPSS in running batch file. So I think I'm trying to do the same with SAS. I'm on Windows.

 

A bit more about the context, before this step it's data management that filters out some observation with data entry problems. The image files mentioned above are the scanned file records. So that when we specify problematic cases, we run the batch file to open the actual materials for data entry and contrast between the two. 

 

 

 

Grand Advisor
Posts: 17,338

Re: How to write batch file in SAS that automates the opening of files?

Can you run x commands and you are on Windows?

 

You can test if you can run X commands with the following code - make sure the path points to your Excel.exe file. Running the following program from SAS will open the Excel application.

 

x "C:\Program Files\Office14\Excel.exe";

Frequent Contributor
Posts: 75

Re: How to write batch file in SAS that automates the opening of files?

Yes, I try and can run x command. I've done with two commands:

 

x 'copy "X:\Permanent data\Image 1.TIF" "E:\Temporary data\Temp 1.tiff"';

 

x '"C:\Program Files\Microsoft Office\Office14\OIS.exe" "E:\Temporary data\Temp 1.tiff"';

 

The first copies one image file from the permanent directory to a temporary directory, the second open the new copied file. So these two lines sort of complete one "loop" in the process.

What I still don't know how to do is to make this process work automatically through all the image files. That is, again, suppose there's variable VAR1 indicating the directory of permanent files (e.g., X:\Permanent data\Image 1.TIF as seen above). I need to copy those files to a temporary folder and open them one by one.

 

Grand Advisor
Posts: 17,338

Re: How to write batch file in SAS that automates the opening of files?

I have no idea what you'd do with that many files open at once. I'd highly suggest going through a process to embed them in a Word or PDF document instead.  That being ... to solve your current problem, create a macro that takes the path of the directory and then use call execute to execute said macro. 

 

The code below is a skeleton, I'm fairly sure the macro variables won't resolve properly and that you'll need to play around with quotes to get the x commands to work properly. 

Get the macro to work with the macro variables then the call execute. 

 

As an aside, you do know that you can select all files in a folder and open them at once, and then tab through them in windows using the left arrow key?

 

%macro open_images(dir);
data _null_;
file=scan("&dir", -1, "\");
call symputx(file_name, file);
run;

x copy "&dir" "E:\Temporary Data\&file_name";

x '"C:\Program Files\Microsoft Office\Office14\OIS.exe" "E:\Temporary data\&file_name"';

%mend;

data _null_;
set dir_list;

str='%open_images('||dir||');';
call execute(str);
run;

 

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to write batch file in SAS that automates the opening of files?

Hi,

 

Can you explain the process and reasoning in a bit more depth for me.  Why is your data management or you attempting to compare pictures of data which should be in a data collection module.  The sheer complexity of comparisons of pictures should be enough to show it is not a viable method.  If you absolutely have to - i.e. you have tried every other method, including writing it out by hand into a spreadsheet (and seriously that would be better than comparing images) - then convert either the whole set or individual to PDF - using PDF's inbuilt batch mode (i.e. select all files, right click, convert to PDF).  Then use the PDF product to do the comparison, do note how that a pixel off in either one, or tiny little changes can show the whole thing to be different, again this is because it is not recommend in any way shape or form to do it this way.  Where is the data, look at that, if its not in a databasable format, then get data entry to enter and validate it - this process would fail most QC checks by the way.

Frequent Contributor
Posts: 75

Re: How to write batch file in SAS that automates the opening of files?

The whole process is like this:

 

I conduct a paper-based survey, it’s just one page let’s say. Responses of 1000 participants are scanned to images and saved. A friend helps me enter data.

 

Now I want to check the data entry quality. I do so by looking at outliers on one variable. I find out that there are 50 out of 1000 cases that seem to be outliers. I have their IDs, and a variable indicating the directory of the responses in scanned images. On a side note, the naming of the images is arbitrary/random as a result of scanning process and has no relation to the IDs.

 

What I want to do is to open these images, look at the original responses and see if there’s an error in data entry. As I have a list of those 50 images, one way is to search for their name manually in the directory, one by one, and open them and do the comparison.

 

But would it be better if we can have some way to do it more automatically? That’s what the SPSS syntax above does. It creates a batch file and, upon executing that file, it opens one image in the list of those 50 images. So I can compare the entered with original values. When I’m done, I close that image, and right after that another image is open thanks to the batch file. So it’s not that 50 images are open at once, it’s instead one after another. So it saves us effort in searching and opening images manually.

 

I think it can be done in a similar way in SAS (as I believe SAS can do almost whatever SPSS does when it comes to programming). It doesn’t have to be a batch file if there’s another solution that brings about the same feature and convenience. I feel like I’m close to it with the two x commands above. What I still need to do is to make it work automatically, in a loop fashion to go over all the images in the list.

Grand Advisor
Posts: 17,338

Re: How to write batch file in SAS that automates the opening of files?

Use SAS to copy the requested files to the temporary folder. Then you can use that trick I mentioned of opening the pics one by one using Windows functionality, you don't need to write any code for that. 

 

Or, as mentioned, write some code that then takes those images and embeds them in a word/PDF file so you can just flip pages.

 

 

Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to write batch file in SAS that automates the opening of files?

As I previously noted, combine them into one PDF file, you can then Cntrl+F + ID to find each image you want to look at.  You may need to convert to text - its pretty simple.

Frequent Contributor
Posts: 75

Re: How to write batch file in SAS that automates the opening of files?

[ Edited ]

This is what I figured out in terms of creating a batch file. Sorry for being stubborn, but it works as I want it to, so it's good anyway Smiley Happy I will also examine your suggested programming and approach.

 

data batwide;set have;

echo = '@echo SCQ ID '||ohhscqid;

predir = 'copy '||'"'||strip(dir5)||strip('\')||strip(tiff5)||strip('"');

preexec = '@'||strip('"')||strip('C:\Program Files\Microsoft Office\Office14\OIS.exe')||strip('"');

temp = '"'||strip('c:\temp\temp.tiff')||strip('"');

run;

 

data batwide; set batwide;

dir = catx(' ',predir,temp);

exec = catx(' ',preexec,temp);

run;

 

data batlong;set batwide;

format bat $200.;

bat = echo;output;

bat = dir;output;

bat = exec;output;

keep bat;

run;

 

data _null_;

set batlong;

file "E:\SAS codes and files\batchfile.bat";

put bat;

run;

Solution
‎02-14-2016 08:01 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: How to write batch file in SAS that automates the opening of files?

 I am unable to check this at the moment, as in meeting, but if your dead set on going that route against all advice, then you can trim your code down (use functions cats/x/t and quote rather than lines of quotes):

data _null_;
  set have;
  file "E:\SAS codes and files\batchfile.bat";
  put cats('@echo SCQ ID ',ohhscqid);
  put cats('copy ',quote(cats(dir5,'\',tiff5')));
  put cats('@',quote('C:\Program Files\Microsoft Office\Office14\OIS.exe'));
  put quote('c:\temp\temp.tiff');
run;

 

 

 

 

 

Frequent Contributor
Posts: 75

Re: How to write batch file in SAS that automates the opening of files?

Thanks for your help.

Super User
Super User
Posts: 6,326

Re: How to write batch file in SAS that automates the opening of files?

You would probably be better served to generate an HTML file from the data and then open the generated file in your browser.

 

 

☑ This topic is SOLVED.

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

Discussion stats
  • 13 replies
  • 696 views
  • 7 likes
  • 4 in conversation