BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lavm01
Calcite | Level 5
Libname Libsas 'E:\libsas\Itop';
Filename FichCsv2 'O:\Sc-Performance\PFI\ITOP\input_data\SCCM\Itop_Extraction_SCCM2012_lab.csv' ENCODING="utf-8";

%macro import_csv(outf,outds);
 %put outf=&outf;
 %let filrf=myfile;
 %put filrf=&filrf;
 %if %sysfunc(fileexist(&outf)) %then %do;
 %let rc=%sysfunc(filename(filrf,&outf));
 %let fid=%sysfunc(fopen(&filrf));
  %if &fid > 0 %then %do;
   %let rc=%sysfunc(fread(&fid));
   %let rc=%sysfunc(fget(&fid,mystring));
   %let rc_close=%sysfunc(fclose(&fid));
    %if &rc = 0 %then %do;
       proc import file="&outf." out=&outds
                   DBMS=DLM REPLACE;
				   Delimiter=';';
                   Guessingrows=32767;
                   Getnames=Yes;
       run;
    %end;
    %else %do;
      %put Fichier &outf. importation impossible fichier vide.;
	  Data _NULL_;
	   Date = today();
	   Format Date yymmdd10.;
	   File Sortie Notitles Mod;
	   put @50 "En date du: " Date +(-1)"," /;
	   put @10 "Le fichier &outf est vide.";
	  run;
	%end;
  %end;
 %let rc=%sysfunc(filename(filrf));
 %end;
 %else %put fichier existe pas;
%mend import_csv;



%import_csv(O:\Sc-Performance\PFI\ITOP\input_data\SCCM\Itop_Extraction_SCCM2012_lab.csv,Libsas.ItopSCCMLAB);
*%import_csv("&Fich2.",Libsas.ItopSCCMLAB);

Can not pass the filename with encoding. is there a way to pass him the filename or some other way because I have to keep the encoding.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

The file exist portion doesn't need to change, they can be mutually exclusive events. 

 

  • Add a parameter to the macro definition that can be the encoding. 
  • Create a filename reference with the encoding, using the file reference and the encoding reference. 
  • Check file exist - no change here.
  • Change the IMPORT step to use the filename reference instead of the filename. 

View solution in original post

7 REPLIES 7
Reeza
Super User

What happens if you use the filename reference instead of the file path?

 

       proc import file=fichCsv2 out=&outds
                   DBMS=DLM REPLACE;
				   Delimiter=';';
                   Guessingrows=32767;
                   Getnames=Yes;
       run;

@lavm01 wrote:
Libname Libsas 'E:\libsas\Itop';
Filename FichCsv2 'O:\Sc-Performance\PFI\ITOP\input_data\SCCM\Itop_Extraction_SCCM2012_lab.csv' ENCODING="utf-8";

%macro import_csv(outf,outds);
 %put outf=&outf;
 %let filrf=myfile;
 %put filrf=&filrf;
 %if %sysfunc(fileexist(&outf)) %then %do;
 %let rc=%sysfunc(filename(filrf,&outf));
 %let fid=%sysfunc(fopen(&filrf));
  %if &fid > 0 %then %do;
   %let rc=%sysfunc(fread(&fid));
   %let rc=%sysfunc(fget(&fid,mystring));
   %let rc_close=%sysfunc(fclose(&fid));
    %if &rc = 0 %then %do;
       proc import file="&outf." out=&outds
                   DBMS=DLM REPLACE;
				   Delimiter=';';
                   Guessingrows=32767;
                   Getnames=Yes;
       run;
    %end;
    %else %do;
      %put Fichier &outf. importation impossible fichier vide.;
	  Data _NULL_;
	   Date = today();
	   Format Date yymmdd10.;
	   File Sortie Notitles Mod;
	   put @50 "En date du: " Date +(-1)"," /;
	   put @10 "Le fichier &outf est vide.";
	  run;
	%end;
  %end;
 %let rc=%sysfunc(filename(filrf));
 %end;
 %else %put fichier existe pas;
%mend import_csv;



%import_csv(O:\Sc-Performance\PFI\ITOP\input_data\SCCM\Itop_Extraction_SCCM2012_lab.csv,Libsas.ItopSCCMLAB);
*%import_csv("&Fich2.",Libsas.ItopSCCMLAB);

Can not pass the filename with encoding. is there a way to pass him the filename or some other way because I have to keep the encoding.

 



 

lavm01
Calcite | Level 5

I know, I already tried that. Me it's more in the macro, the outf that does not recognize the encoding.

lavm01
Calcite | Level 5
%import_csv(FichCsv2,Libsas.ItopSCCMLAB);

 

 

I tried with that but it does not work.

Reeza
Super User

I can only comment on what you've posted.

Your encoding is not a parameter within PROC IMPORT that I can see, and you don't pass it in any form so I'm not sure what you're expecting. I don't see the encoding in the macro anywhere...and the section below - myfile is never defined within what you've posted so that may be problematic. 

 

You could declare a filename statement within the macro and use that similar to the code I posted earlier, instead of passing the path to PROC IMPORT, pass the filename reference instead.

 

 %put outf=&outf;
 %let filrf=myfile;
 %put filrf=&filrf;

 


@lavm01 wrote:

I know, I already tried that. Me it's more in the macro, the outf that does not recognize the encoding.


 

lavm01
Calcite | Level 5
 
I agree with you, but the marcro checks the file and also checks if it is possible to import the file. That is why the outf contains the path of the CSV as well as the encoding.
Reeza
Super User

The file exist portion doesn't need to change, they can be mutually exclusive events. 

 

  • Add a parameter to the macro definition that can be the encoding. 
  • Create a filename reference with the encoding, using the file reference and the encoding reference. 
  • Check file exist - no change here.
  • Change the IMPORT step to use the filename reference instead of the filename. 
Tom
Super User Tom
Super User

Your macro is already generating one or more steps, so just use a data step instead of macro code to call the various functions, (fileexist(),filename(),fopen(),etc.).

 

It will be a LOT easier to debug.

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!

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
  • 7 replies
  • 747 views
  • 0 likes
  • 3 in conversation