Hi,
I'm trying to find a piece of script that will apply some simple calculations to the entire contents of a SAS library (2,800 files, all of which have the same columns, and are named "_YYYYMMDD").
The calculation itself is straight forward (create a column based on an existing one, change any char values to numeric, and store new column as numeric). I've built this, however I can't find any method of running this on the contents of the library.
I've created a Dictionary which lists all data within the library as follows:
proc sql ;
create table work.mytables as
select *
from dictionary.tables
where libname = 'B'
order by memname ;
quit ;
Anyone have any ideas how to proceed from here? Thanks.
Using SAS EG8.1
Hi @gsisme ,
Try CALL EXECUTE():
data testSet1 testSet2;
variable = 42;
run;
data YOUR_DICTIONARY;
datasetnameVariable = 'testSet1'; output;
datasetnameVariable = 'testSet2'; output;
run;
filename t TEMP;
data _null_;
file t;
input;
put _infile_;
cards4;
/* put your code here */
/*e.g.*/
variable = 100 * variable;
/* put your code here */
;;;;
run;
data _null_;
set YOUR_DICTIONARY;
call execute('data ' || datasetnameVariable || ';');
call execute('set ' || datasetnameVariable || ';');
call execute('%nrstr(%include t;) run;');
run;
All the best
Bart
Are these .txt files? Are these .csv files? Can you give us more information? How are you reading them?
If they are .txt files and maybe if they are .csv files, you can read them all with one SAS data step.
Apologies, I've imported them to the SAS library and they're stored as SAS files.
@gsisme wrote:
Apologies, I've imported them to the SAS library and they're stored as SAS files.
If they WERE .txt files or .csv files, you still wouldn't need a loop and you wouldn't need the individual SAS data sets. You could read them all with a single DATA step.
@gsisme wrote:
Apologies, I've imported them to the SAS library and they're stored as SAS files.
I'm going to take this as meaning that you used proc import. Did you check the status of the variables as they were imported?
Did you import some sort of text files such as delimited with spaces, commas, tabs or other characters? Were any of these files supposed to have the same structure (same variables, in same order?).
If so then you really need to read the data with data step code to control the contents. Especially if you had a description document that would indicate what the maximum length of variables would be and the purpose of variables.
Prevention by reading properly in the first place is much better than trying to fix things later.
Also you can add the calculation to the step that reads the data.
I would be extremely cautious of any process that does what you ask because it has the potential to cause other issues. There many types of identifiers used that contain only digits but should not be treated as numeric. Sometimes account "numbers" will exceed the length that SAS will store properly. And account numbers should not have any numeric calculations done with them in general.
Is this homework or something?
Hi @gsisme ,
Try CALL EXECUTE():
data testSet1 testSet2;
variable = 42;
run;
data YOUR_DICTIONARY;
datasetnameVariable = 'testSet1'; output;
datasetnameVariable = 'testSet2'; output;
run;
filename t TEMP;
data _null_;
file t;
input;
put _infile_;
cards4;
/* put your code here */
/*e.g.*/
variable = 100 * variable;
/* put your code here */
;;;;
run;
data _null_;
set YOUR_DICTIONARY;
call execute('data ' || datasetnameVariable || ';');
call execute('set ' || datasetnameVariable || ';');
call execute('%nrstr(%include t;) run;');
run;
All the best
Bart
Other option would be to use %macro instead filename,
%macro code();
/* put your code here */
/*e.g.*/
variable = 100 * variable;
/* put your code here */
%mend code;
data _null_;
set YOUR_DICTIONARY;
call execute('data ' || datasetnameVariable || ';');
call execute('set ' || datasetnameVariable || ';');
call execute('%nrstr(%code()) run;');
run;
All the best
Bart
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!
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.