DATA Step, Macro, Functions and more

Problem macro import_csv with filename encoding uft-8

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Problem macro import_csv with filename encoding uft-8

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.

 


Accepted Solutions
Solution
‎01-29-2018 07:52 AM
Super User
Posts: 24,004

Re: Problem macro import_csv with filename encoding uft-8

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


All Replies
Super User
Posts: 24,004

Re: Problem macro import_csv with filename encoding uft-8

[ Edited ]

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.

 



 

New Contributor
Posts: 4

Re: Problem macro import_csv with filename encoding uft-8

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

New Contributor
Posts: 4

Re: Problem macro import_csv with filename encoding uft-8

%import_csv(FichCsv2,Libsas.ItopSCCMLAB);

 

 

I tried with that but it does not work.

Super User
Posts: 24,004

Re: Problem macro import_csv with filename encoding uft-8

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.


 

New Contributor
Posts: 4

Re: Problem macro import_csv with filename encoding uft-8

 
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.
Solution
‎01-29-2018 07:52 AM
Super User
Posts: 24,004

Re: Problem macro import_csv with filename encoding uft-8

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. 
Super User
Super User
Posts: 8,279

Re: Problem macro import_csv with filename encoding uft-8

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.

☑ This topic is solved.

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

Discussion stats
  • 7 replies
  • 95 views
  • 0 likes
  • 3 in conversation