BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
David_Billa
Rhodochrosite | Level 12

I've the below list of files in one folder. Now I want to create macro variables based on the last  four digits. of each file.

 

INS_GT_1_1000.csv
INS_GT_1_1001.csv
INS_GT_1_1002.csv

 

I will define the folder name in macro variable as below. Now I want to scan this directory and get the last four digits of each file and store it in macro variables. For each file, I need to create a macro variable which has the value of four digits from the file name.

 

%let dir=/var/data/Server/

 

Excepted Results:

 

First macro variable (e.g. ID1) should have value as 1000, Second macro variable (e.g. ID2)  should have value as 1001 and Third macro variable (e.g. ID3) should have value as 1002

 

Any help?

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Use the file functions to retrieve the filenames, then some string functions to extract the digits:

%let dir=/var/data/Server/;

data files;
fref = "DIR";
if filename(fref,"&dir.") = 0
then do;
  did = dopen(fref);
  if did ne 0
  then do;
    do i = 1 to dnum(did);
      name = dread(did,i);
      if upcase(scan(name,-1,".")) = "CSV" then output;
    end;
    did = dclose(did);
  end;
  rc = filename(fref);
end;
keep name;
run;

data _null_;
set files;
digits = substr(name,length(name)-7,4);
call symputx(cats("id",_n_),digits);
run;

View solution in original post

28 REPLIES 28
PeterClemmensen
Tourmaline | Level 20

Would this include CSV files only?

 

And what if a file does not end with a digit?

David_Billa
Rhodochrosite | Level 12
Only CSV files will be placed in the folder and it will have digits.
David_Billa
Rhodochrosite | Level 12

Only CSV files will be placed in the Folder and it will have Digits.

Shmuel
Garnet | Level 18

I suppose this post is a continuation or an alternative way to solve your issue posted in 

https://communities.sas.com/t5/SAS-Programming/Conditionally-move-the-files-based-on-macro-variable/... 

 

I don't think this alternative will be easier or shorter to do the desired work.

 

 

 

 

Kurt_Bremser
Super User

Use the file functions to retrieve the filenames, then some string functions to extract the digits:

%let dir=/var/data/Server/;

data files;
fref = "DIR";
if filename(fref,"&dir.") = 0
then do;
  did = dopen(fref);
  if did ne 0
  then do;
    do i = 1 to dnum(did);
      name = dread(did,i);
      if upcase(scan(name,-1,".")) = "CSV" then output;
    end;
    did = dclose(did);
  end;
  rc = filename(fref);
end;
keep name;
run;

data _null_;
set files;
digits = substr(name,length(name)-7,4);
call symputx(cats("id",_n_),digits);
run;
David_Billa
Rhodochrosite | Level 12
Thank you. May I know why you have used the variable 'fref' in your
program?
Kurt_Bremser
Super User

It's a matter of habit; when FILENAME is used in a macro, it HAS to be a macro variable and not a string. So I also do the same in a data step.

By using an empty variable of length 8, SAS will assign a name automatically.

David_Billa
Rhodochrosite | Level 12

I have one more question for you. I want to use the created macro variables (ID1 - IDn) in other macro under %index function.

Like this,
%index(file_name,‹macrovariable ID1 - IDn>) > 0 then %do
.
.
%end;
May I know how should I use the created macro variables under %index function? 
Tom
Super User Tom
Super User

You can reference a macro variable's value by writing the name of the macro variable prefixed with the & character.

 

What is it you are actually trying to do?  Perhaps the macro variables are not even required to do what you want.

David_Billa
Rhodochrosite | Level 12

Appreciate if you could help me with example for this, 'You can reference a macro variable's value by writing the name of the macro variable prefixed with the & character.'

Tom
Super User Tom
Super User

So if you create a macro variable named FILE and want to use the value of FILE in your program you type &FILE.  You can add a period at the end of the name to be explicit about where the name ends.

Here is a simple example:

%let name=filename1.xls ;
%let number=1 ;
%if %index(&name,&number) %then %do;
  %put &number is contained in &name.. ;
%end;
Kurt_Bremser
Super User

So you do not need a list of macro variables, but a macro variable with a value, which you want to search for in whatever is represented by file_name here.

 


@David_Billa wrote:

I have one more question for you. I want to use the created macro variables (ID1 - IDn) in other macro under %index function.

Like this,
%index(file_name,‹macrovariable ID1 - IDn>) > 0 then %do
.
.
%end;
May I know how should I use the created macro variables under %index function? 

 

David_Billa
Rhodochrosite | Level 12

@Tom @Kurt_Bremser 

 

I need a  need a list of macro variables, but a macro variable with a value and b macro variable with b value and so on.

So how to pass these macro variables as arguments to %index function?

Kurt_Bremser
Super User

You start with a bunch of year/month values, and you want to compare those with another value; please describe what you want to achieve here, the big picture, so to say.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 28 replies
  • 4233 views
  • 13 likes
  • 6 in conversation