BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Chelsea8
Calcite | Level 5
I am pulling 11 csv files over from my c: and the first row is not becoming the sas column names. The first field name is 'sep=' and the following field names are 'var1, var2, var3....etc'. I've tried using 'getnames' and the column names are still sep=, VAR1, VAR2...ect. Also the log shows informat as sep=n, VAR1, VAR2...etc. I've attached a test file as an example of my data. I'm not sure how to get SAS to grab the first row and make them column names. 
 Part of the log: 
informat "sep="N $12. ;
678 informat VAR2 $94. ;
679 informat VAR3 $16. ;
680 informat VAR4 $11. ;
681 informat VAR5 $11. ;
682 informat VAR6 $11. ;
683 informat VAR7 $11. ;
 
 
Import code: 
 
%macro drive(dir,ext); 
   %local cnt filrf rc did memcnt name;
 
   %let cnt=0;
   %let filrf=mydir;
 
   %let rc=%sysfunc(filename(filrf,&dir));
   %let did=%sysfunc(dopen(&filrf));
 
   %if &did ne 0 %then %do;
      %let memcnt=%sysfunc(dnum(&did));
      %do i=1 %to &memcnt;
         %let name=%qscan(%qsysfunc(dread(&did,&i)),-1,.);
         %if %qupcase(%qsysfunc(dread(&did,&i))) ne %qupcase(&name) %then %do;
            %if %superq(ext) = %superq(name) %then %do;
               %let cnt=%eval(&cnt+1);
               %put %qsysfunc(dread(&did,&i));
               proc import datafile="&dir\%qsysfunc(dread(&did,&i))" out=dsn&cnt
                  dbms=csv replace;
getnames=yes;
               run;
            %end;
         %end;
       %end;
    %end;
  %else %put &dir cannot be opened.;
 
  %let rc=%sysfunc(dclose(&did));
%mend drive;
 
%drive(C:\Users\Chelsea.H\Desktop\QSS_2022,csv);
1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20
Also make sure you have set VALIDVARNAME=ANY.
Data never sleeps

View solution in original post

4 REPLIES 4
JosvanderVelden
SAS Super FREQ

Can you import a single csv-file using proc import, without using the macro, and get the columnnames?

NOTE: I and many others don't open attached files because of security implications. Please include the first 3 lines of the csv (with only 3 or 4 columns) as code in the post so we can have a look.

LinusH
Tourmaline | Level 20
Also make sure you have set VALIDVARNAME=ANY.
Data never sleeps
Chelsea8
Calcite | Level 5
Thank you! I have multiple people who have helped and this suggestion was the missing piece.
Tom
Super User Tom
Super User

Which 11 files?  Which ONE of the 11 files is the one that is generating a variable name of SEP= ?

 

You appear to be using a complex macro code to get the list of files.  

If the list is known just type (or copy and paste) the filenames.

If the list is unknown and you need to use DREAD() to find the file names why are you doing it in macro code instead of in a dataset?  It is MUCH MUCH MUCH easier to debug normal SAS code than macro code.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 453 views
  • 1 like
  • 4 in conversation