BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
FredLag
Calcite | Level 5

Hi Sas community,

 

I am new to the sas software so forgive me if I make rookie mistakes. I've got multiple CSV file that I want to import in SAS with a macro-function.

All those files names end with "_2018" that I want to remove when importing datafile.

Some files names have only one underscore, and some have two. Example :

games_2018

box_scores_2018

 

Here's what I got so far :

 

%macro import(devoir1);

FILENAME REFFILE "C:\mypath\&devoir1..csv";

	PROC IMPORT
		DATAFILE=REFFILE 
		DBMS=CSV
		OUT= %sysfunc(compress(&devoir1. ,"_2018")) REPLACE; 
		GETNAMES=YES;
	RUN;

%mend import;

%import (box_scores_2018);

So this code works, but remove all "_" from the name, so that "box_score_2018" is import as "boxscore". I would like to keep the first on to get "box_score".

 

If someone has an idea on how to do this, it would be very appreciated!

Thanks a lot!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Do you always want to remove the last 5 characters?

%let dsn=%substr(&devoir1,1,%length(&devoir1)-5);
...
out=&dsn

Or do you want to remove anything after the last underscore?

%let dsn=&devoir1;
%if %index(&dsn,_)>1 %then 
  %let dsn=%substr(&dsn,1,%length(&dsn)-%length(%scan(&dsn,-1,_))-1)
;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

Do you always want to remove the last 5 characters?

%let dsn=%substr(&devoir1,1,%length(&devoir1)-5);
...
out=&dsn

Or do you want to remove anything after the last underscore?

%let dsn=&devoir1;
%if %index(&dsn,_)>1 %then 
  %let dsn=%substr(&dsn,1,%length(&dsn)-%length(%scan(&dsn,-1,_))-1)
;
FredLag
Calcite | Level 5
The first option! Thanks a lot!!
PaigeMiller
Diamond | Level 26

You probably could use the FIND function to find the string '_2018', and then delete everything before that.

 

%macro import(devoir1);
data _null_;
    z=find("&devoir1",'_2018');
    new_name=substr("&devoir1",1,z-1);
    call symputx('new_name',new_name);
run;

FILENAME REFFILE "C:\mypath\&devoir1..csv";

	PROC IMPORT
		DATAFILE=REFFILE 
		DBMS=CSV
		OUT=&new_name REPLACE; 
		GETNAMES=YES;
	RUN;

%mend import;

%import (box_scores_2018)
--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 481 views
  • 1 like
  • 3 in conversation