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

Imagine that I have a lot of dbf files in different folders. For example, in d:\test\A, I have af.dbf, sh.dbf, and syhd.dbf. And in d:\test\B, I have ujs.dbf, i.dbf, qye.dbf, and ysuje.dbf. ... So, random large numbers of dbf files are distributed across some random numbers of folders under the d:\test.

For all the dbf.files, there is a field (say, CODE). I want to add a field (say, CODE4), extract the last four digits of the values in CODE, and save them to the CODE4 as character values. So, it looks like as follows:

CODE               CODE4

8273123878       3878

9182787386       7386

0182730099       0099

61267198           7198

...                     ...

How can I automate that process for all the dbf files? Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

The simplese way is use PIPE function to gain the power of OS command.

filename x pipe 'dir c:\test\*.txt /s /b'; * list all of txt files under c:\test;
data _null_;
 infile x length=len;
 input fname $varying200. len;
 call execute('proc import datafile="'||fname||'" out='||strip(scan(fname,-2,".\"))||' dbms=dlm replace;getnames=yes;delimiter=" ";run;');
 call execute('data '||strip(scan(fname,-2,".\"))||'; set '||strip(scan(fname,-2,".\"))||';code4=substr(code,length(code)-3);run;');
run;

Ksharp

View solution in original post

1 REPLY 1
Ksharp
Super User

The simplese way is use PIPE function to gain the power of OS command.

filename x pipe 'dir c:\test\*.txt /s /b'; * list all of txt files under c:\test;
data _null_;
 infile x length=len;
 input fname $varying200. len;
 call execute('proc import datafile="'||fname||'" out='||strip(scan(fname,-2,".\"))||' dbms=dlm replace;getnames=yes;delimiter=" ";run;');
 call execute('data '||strip(scan(fname,-2,".\"))||'; set '||strip(scan(fname,-2,".\"))||';code4=substr(code,length(code)-3);run;');
run;

Ksharp

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
  • 1 reply
  • 689 views
  • 0 likes
  • 2 in conversation