BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacksonan123
Lapis Lazuli | Level 10

I have a file Nflag which looks like this.

Flag

0

1

0

1

1

I also have a group of folders named:

Test1.nm7

Test2.nm7

Test3.nm7

Test4.nm7

Test5.nm7

What I would like to do is to have the folder names reflect the flag values so that those with a 0 (e.g.,0Test1.nm7)  designation can be later deleted.  What I would have initially would be:

0Test1.nm7
1Test2.nm7
0Test3.nm7
1Test4.nm7
1Test5.nm7

 

The final  folders would be:

1Test2.nm7
1Test4.nm7
1Test5.nm7

 

Can someone tell me how best to do this?

1 ACCEPTED SOLUTION

Accepted Solutions
jacksonan123
Lapis Lazuli | Level 10

This solution worked.

View solution in original post

12 REPLIES 12
SuryaKiran
Meteorite | Level 14

Hello,

 

Not sure exactly what your trying to achieve here. Do you want to modify the values in your dataset or you really want to rename the folders?

 

Thanks,
Suryakiran
Ksharp
Super User

Create a BAT file(Windows OS) by sas. like:

 

data _null_;

 set have;

file 'c:\temp\rename.bat';

x=' rename  Test1.nm7  0Test1.nm7 ';  /*<-- Change it based on HAVE dataset*/

put x;

run;

 

 

And execute c:\temp\rename.bat under Command Line (Start--> Run --> cmd  )

jacksonan123
Lapis Lazuli | Level 10
I understand the concept of the batch file but I am unsure how it is
supposed to work.

Let me explain again. I have two folders one Nflag which has the variable
flag.

Flag

0

1

0

A second folder bootstrap contains folders named :

Test1.nm7

Test2.nm7

Test3.nm7

In your batch file let's assume that my bootstrap is named have. We
activate the bat file.

My question is when do we bring in Nflag which contains the values to be
concatenated 1 to 1 with Test1.nm7 .. Can you tell me how that file enters
into your solution?


Ksharp
Super User

OK. assuming you have a folder named c:\bootstrap ,

under it there are three sub-folders named : 


Test1.nm7
Test2.nm7
Test3.nm7

 

Firstly , make a table contains 0 and 1.

 

data have;
input Flag;
cards;
0
1
0
;

Secondly, get the sub-folders name :

 


data folders;
 rc=filename('x','c:\bootstrap');
 did=dopen('x');
 do i=1 to dnum(did);
  folder=dread(did,i);
  output;
 end;
 run;

Lastly ,make a BAT file if you could and execute  BAT file.

Here is c:\temp\rename.bat .

Enter command line and type " c:\temp\rename.bat" to execute it .

 


 filename x 'c:\temp\rename.bat';
 data _null_;
 merge have folders;
 rename=catx(' ','rename',folder,cats(flag,folder));
 file x;
 if _n_=1 then put 'cd c:\bootstrap';
 put rename;
 run;
jacksonan123
Lapis Lazuli | Level 10
I ran the code you supplied but I got an error. This is the log.

OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;

72

73

74 data have;

75 input Flag;

76 cards;

NOTE: The data set WORK.HAVE has 3 observations and 1 variables.

NOTE: DATA statement used (Total process time):

real time 0.01 seconds

cpu time 0.01 seconds





80 ;

81

82 data folders;

83 rc=filename('x','c:\bootout');

84 did=dopen('x');

85 do i=1 to dnum(did);

86 folder=dread(did,i);

87 output;

88 end;

89 run;

NOTE: Argument 1 to function DNUM(0) at line 85 column 12 is invalid.

ERROR: Invalid DO loop control information, either the INITIAL or TO
expression is missing or the BY expression is missing, zero,

or invalid.

rc=0 did=0 i=1 folder= _ERROR_=1 _N_=1

NOTE: Mathematical operations could not be performed at the following
places. The results of the operations have been set to

missing values.

Each place is given by: (Number of times) at (Line):(Column).

1 at 85:12

NOTE: The SAS System stopped processing this step because of errors.

WARNING: The data set WORK.FOLDERS may be incomplete. When this step was
stopped there were 0 observations and 4 variables.

WARNING: Data set WORK.FOLDERS was not replaced because this step was
stopped.

NOTE: DATA statement used (Total process time):

real time 0.01 seconds

cpu time 0.00 seconds








Ksharp
Super User

Did you create a folder named "bootout" under C:\ ?

 

c:\bootout is refer to a directory.

 

and under it ,create many sub-directory like Test1.nm7.

c:\bootout\Test1.nm7

c:\bootout\Test2.nm7

.........

 

 

jacksonan123
Lapis Lazuli | Level 10

This solution worked.

jacksonan123
Lapis Lazuli | Level 10
I ran the batch file from a folder temp with a command prompt with the
command c:\temp\rename.bat, I get several messages.

They are( filename is not recognized as an internal or external command,
operable program or batch file). I get the same for data null , and merge.

For rename=catx(' ','rename',folder.cats(flag,folder)); I get the syntax of
the command is incorrect.

It worked but this time, I must have done something wrong. I have folder
bootstrap with the test*.nm7 folders and a folder bootout. Where should it
be located?

Rename.bat

Filename x 'c:\temp\rename.bat';

Data _null_;

Merge have folders;

rename=catx(' ','rename',folder.cats(flag,folder));

File x;

If _n_ then put 'cd:\bootstrap';put rename;run;
Ksharp
Super User

Can you post your ERROR information?

 

Did your directory name contains some special character like blank ? if it was ,you need quote around it to rename.

 

rename "1Test2 nm7" 1Test2.nm7

jacksonan123
Lapis Lazuli | Level 10
Thanks for responding, that was the issue and all is well.

Happy New Year.
ChrisNZ
Tourmaline | Level 20
Like this?

data _null_;
set HAVE;
CMD=catt('rename "', FNAME, '" "', FLAG, NAME, '"');
putlog CMD;
* call system(CMD);
run;

Uncomment the last line when the code shown in the log is satisfactory.
jacksonan123
Lapis Lazuli | Level 10
I ran your code and it ran okay. Not stated but I guess this should be run
from a DOS window under a *.bat file. My question is where is the Flag data
concatenated with the

Test1.nm7, Test2.nm7, Test3.nm7 which are represented by FNAME and NAME, how
is that input into the code?

I ask because I will actually have 1000 of the Testi.nm7 files to rename.

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!

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