Desktop productivity for business analysts and programmers

Importing the data from zipped csv file

Accepted Solution Solved
Reply
Regular Contributor
Posts: 188
Accepted Solution

Importing the data from zipped csv file

I am trying to import the csv data in zipped file using:

 

filename foo ZIP '/applocation/kg/label_categories.csv.zip' member="label_categories.csv" ;

 

proc import datafile=foo out=label dbms=csv replace; getnames=yes;run;

 

But the dataset contains only 1 variable with 1 length.

 

I know, I can read the data using: input statement. But that requires me to specify manually input for all variables, which can be tiresome a job while importing lot of files.

 

How can I get all the data similar to proc import, without using the input statement?


Accepted Solutions
Solution
‎07-14-2016 09:05 AM
Community Manager
Posts: 2,884

Re: Importing the data from zipped csv file

[ Edited ]

Try this: use DATA step to "copy" the file out of the ZIP first, then import it.  I describe this in a blog post about FILENAME ZIP.

 

Your code might look like:

 

filename inzip ZIP '/applocation/kg/label_categories.csv.zip' 

filename csv "%sysfunc(getoption(work))/label_categories.csv" ;

data _null_;
   /* using member syntax here */
   infile inzip(label_categories.csv) 
       lrecl=256 recfm=F length=length eof=eof unbuf;
   file csv lrecl=256 recfm=N;
   input;
   put _infile_ $varying256. length;
   return;
 eof:
   stop;
run;

proc import datafile=csv out=label dbms=csv replace; getnames=yes;run;

View solution in original post


All Replies
Solution
‎07-14-2016 09:05 AM
Community Manager
Posts: 2,884

Re: Importing the data from zipped csv file

[ Edited ]

Try this: use DATA step to "copy" the file out of the ZIP first, then import it.  I describe this in a blog post about FILENAME ZIP.

 

Your code might look like:

 

filename inzip ZIP '/applocation/kg/label_categories.csv.zip' 

filename csv "%sysfunc(getoption(work))/label_categories.csv" ;

data _null_;
   /* using member syntax here */
   infile inzip(label_categories.csv) 
       lrecl=256 recfm=F length=length eof=eof unbuf;
   file csv lrecl=256 recfm=N;
   input;
   put _infile_ $varying256. length;
   return;
 eof:
   stop;
run;

proc import datafile=csv out=label dbms=csv replace; getnames=yes;run;
Super Contributor
Posts: 414

Re: Importing the data from zipped csv file

This makes me curious to find out what the input file looks like from the SAS perspective.

 

Could you execute this piece of code and share the results?

 

filename foo ZIP '/applocation/kg/label_categories.csv.zip' member="label_categories.csv" ;

data _null_;
   infile foo obs=10;
   input; 
   put _infile_;
run;

 

This dumps a piec of your unzipped file to the log. That may help understand the behaviour of proc import.

 

Regards,

- Jan.

 

 

 

Regular Contributor
Posts: 188

Re: Importing the data from zipped csv file

here is the log:

 

label_id,category
1,
2,game-type
3,game- themes
4,game-Style
5,game-time
6,game-things
7,game-Finding fault
8,game-stress reliever
9,game-pet
NOTE: 10 records were read from the infile FOO.
The minimum record length was 2.
The maximum record length was 22.

 

but the output dataset contains this:

 

Var1

1
2
3
4
5

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 938 views
  • 0 likes
  • 3 in conversation