DATA Step, Macro, Functions and more

File import

Reply
Contributor
Posts: 71

File import

Hi,


I have a PKZIP file(attached) which I manually unzip and read a particular file into SAS. I'm wondering if there a work around in SAS to unzip the file via data step and read the contents. In this case, I want to read dec12.cpt.


PS: I'm using PC SAS on windows to run the above process.


Thanks in advance for your help!

Attachment
Contributor
Posts: 39

Re: File import

You can use a VB script to have it done.

Here is the code.

Fname = ("C:\Users\Desktop\manual\2CBM00176.zip") /* your zip file folder*/

    DirName = ("C:\Users\Desktop\manual") /* your targer folder*/

    Dim oApp As Object

    Set oApp = CreateObject("Shell.Application")

    oApp.Namespace(DirName).CopyHere oApp.Namespace(Fname).items

    Set oApp = Nothing

save it as .vbs file and call it from sas

data _null_;

      x "C:\Users\O568484\Desktop\manual_Marketing\test.vbs";

   run;

filename test "C:\Users\Desktop\manual\dec12.cpt";

%include test;

I hope this will help

Contributor
Posts: 71

Re: File import

Devnand - Thanks for your reply. I followed your instructions but I am getting error message which is as follows.

Line : 3

Char: 14

Error: expected end of statement

code: 800A0401

Source: Microsoft VBscript compliation error

My VB script

Fname = ("C:\ARM\ARMfile.zip") 

    DirName = ("C:\TEMP") 

    Dim oApp As Object

    Set oApp = CreateObject("Shell.Application")

    oApp.Namespace(DirName).CopyHere oApp.Namespace(Fname).items

    Set oApp = Nothing

Respected Advisor
Posts: 3,124

Re: File import

It is a popular practice for SAS users to use PIPE for this kind of task, http://www2.sas.com/proceedings/sugi31/155-31.pdf

While this paper does not specifically spell out how to do it using PKZIP, but it should be easy enough to find it out the right command by typing pkunzip or pkunzip /? or look it up online.

Something along the line of the following may work for you:

filename pkzip pipe 'pkunzip.exe c:\test.zip -c' ;

data want ;

infile pkzip ;

input var & $200. ;

run ;

Haikuo

Contributor
Posts: 71

Re: File import

Hai.Kuo,

I tried your approach before posting the question in this forum and i got the below error


Stderr output:
'pkunzip.exe' is not recognized as an internal or external command,
operable program or batch file.

I tried my best to search online but couldn't find the right pipe command for pkunzip. i even tried the below but getting same error

filename pkzip pipe 'PKUNZIP C:\ARM\ARMfile.zip C:\TEMP -c';

Thanks!

Contributor
Posts: 39

Re: File import

Please remove as object from line 3 of the code.

Contributor
Posts: 71

Re: File import


different error this time:

Line : 5

Char: 5

Error: The system cannot find the file specified

code: 80070002

Source: (null)

Contributor
Posts: 39

Re: File import

Fname = ("C:\Users\\Desktop\New\2CBM00176.zip")

    DirName =("C:\Users\\Desktop\New\")

    Dim oApp

    Set oApp = CreateObject("Shell.Application")

    oApp.Namespace(DirName).CopyHere oApp.Namespace(Fname).items

    Set oApp = Nothing

Please make sure that your code exactly looks like the same. I see backslash missing in the 2nd line of your code.

I hope this will help

Contributor
Posts: 71

Re: File import

For some reason I'm getting the same error again.Below is my code. Can you do a quick check on my code to make sure i am not missing anything?

Fname = ("C:\TEMP\zipcode_Apr13.zip") 

    DirName = ("C:\TEMP\") 

    Dim oApp

    Set oApp = CreateObject("Shell.Application")

    oApp.Namespace(DirName).CopyHere oApp.Namespace(Fname).items

    Set oApp = Nothing

I see double backslash in your code in Fname and Dirname? Is that something i should also have in my code?

Super Contributor
Posts: 333

Re: File import

The pipe commands are specific to PKZIP and assume that it is installed on your machine (as opposed to WinZIp which requires a command line add on and different syntax).

Here is some more reference for the VB script (which assumes you are trying to use the built in windows compression). I seem to remember there are compression algortihms that windows can not uncompress so if the files were not compressed use the default options that may cause trouble.

http://support.sas.com/resources/papers/proceedings12/057-2012.pdf

Hope that helps.

EJ

Contributor
Posts: 71

Re: File import

Thanks for the insight!

Respected Advisor
Posts: 3,124

Re: File import

In your first post, you mentioned that you manually unzip the file. Which program you were using to do that? If it is not pkunzip, then PIPE whatever program available on your machine.

Haikuo

Contributor
Posts: 71

Re: File import

Hai.kuo- I would just right click on the zip file and do extract to manually unzip the file.

Contributor
Posts: 39

Re: File import

SAS ADMIRER:

I am not sure what you are doing wrong. I copied the above code and execute it and it ran like a champ.

Sorry it did not help you.

Contributor
Posts: 71

Re: File import

Devnand- There must be something wrong in my system. Thanks for all your help though!

Ask a Question
Discussion stats
  • 14 replies
  • 389 views
  • 8 likes
  • 4 in conversation