SAS Check the directory exists - sas

How can I check if directory exists and if not, it would be then: syserr <>0?, I need
%sysfunc(filename(fileref,&dir))
I need the syserr value if exists 0 if not <>0.
thanks for help

A cleaner method to check if a folder exists
%let does_it_exist=%sysfunc(fileexist(&dir));
This returns a value of 0 if the folder does not exist, and a value of 1 if the folder does exist.

There may be a cleaner way of getting SAS to throw an error, but the following worked for me. The general idea is that, if the directory exists, you do something that keeps syserr set to 0; if not, you do something that throws an error.
%let your_path = "...";
%macro your_macro(dir);
%let rc = %sysfunc(filename(fileref, &dir.));
%if %sysfunc(fexist(&fileref)) %then %do;
data _null_;
set _null_;
run;
%end;
%else %do;
data _null_;
set something_that_doesnt_exist;
run;
%end;
%put syserr = &syserr.;
%mend your_macro;
%your_macro(&your_path.);

Related

SAS macro if exists

If the dataset dataset_1&x._&y. exists, I want to get data from it, but if it doesn't exist i want to get data from dataset_2.
I have tried the following macro but it doesn't work:
%macro test(x,y);
%if %sysfunc(exist(dataset_1_&x._&y.)) %then %do;
data final_data;
set dataset_1_&x,_&y.;
run;
%end;
%else %do;
data final_data;
set dataset_2;
run;
%end;
%mend;
Try this - no need to define a macro:
%let exist=%sysfunc(exist(work.dataset_1&x._&y.));
%let inds=%sysfunc(ifc(&exist=1,work.dataset_1&x._&y.,work.dataset_2));
data work.final_data;
set &inds;
run;
If you did want a macro to see if a dataset (or view) exists, you could use this one: https://core.sasjs.io/mf__existds_8sas.html

Rename a external file in a SAS macro

I am getting a generic 'Statement not valid or out of order' message with the below:
%macro test;
data _null_;
%if %sysfunc(fileexist("C:\report_201809.xlsx")) = 1 %then %do;
rc=%sysfunc(rename("C:\report_201809.xlsx",
"C:\report_201809.xlsx"_old.xlsx",'file'));
%end;
%mend;
%test;
The code below should get you what you need. While you can use %if statements in a data step you generally won't need to. I'm guessing the error is coming from the %sysfunc function around the fileexist and rename functions. %sysfunc allows you to call data step functions outside of a data step so it is not needed here.
%macro test;
data _null_;
if fileexist("C:\file.txt") then do;
rc = rename("C:\file.txt", "C:\file2.txt", 'file');
end;
run;
%mend;
Alternatively, you could use an X Command that allows you to execute Windows commands. You could replace the rename function with the following statement.
x move C:\file.txt C:\file2.txt;
Remove the DATA _NULL_ or proceed per #J_Lard.
Macro arguments used in %sysfunc invoked function calls are implicitly quoted and do not need additional ' or "
%macro test;
%local rc;
%if %sysfunc(fileexist(C:\report_201809.xlsx)) = 1 %then %do;
%let rc = %sysfunc(rename(C:\report_201809.xlsx,C:\report_201809_old.xlsx,file));
%end;
%test;
You original code may have worked (by way of non-obvious side effect) if the filename "C:\report_201809.xlsx"_old.xlsx" (having an extraneous ") was corrected to "C:\report_201809_old.xlsx"

Check if table exists, if not do nothing

I have a problem with my macro. Try to create a table if the name exists.
%let tableA = Cars;
%let tableB =;
This works:
%macro CREATETABLE(name);
%if %symexist(name) = 1 %then %do;
proc sql;
Create table ....
But if the table name doesnt exists:
%else...( do nothing )
i want SAS to do nothing, but i didnt get it to work. Getting always SAS errors because the table name doenst exists.
%CREATETABLE(CARS)/%CREATETABLE(&tableA) - works, %symexist(chkTabelle) -> 1
%CREATETABEL(asfsf)/%CREATETABLE(&tableB) - error, %symexist(chkTabelle) -> 0
%symexist checks to see if a macro symbol exists. The macro symbol NAME always exists. It sounds like you want to check if a dataset exists. To do that, you use the EXIST() function.
From the docs:
http://support.sas.com/kb/24/670.html
%macro checkds(dsn);
%if %sysfunc(exist(&dsn)) %then %do;
proc print data = &dsn;
run;
%end;
%else %do;
data _null_;
file print;
put #3 #10 "Data set &dsn. does not exist";
run;
%end;
%mend checkds;

SAS 9.4 Macro Eval Function issues

I have a data set called monthlypayments, which is located in a folder I have assigned ‘training’ and it has a variable payments.
I want to output ‘payment type’ which is “high payment” if the payment>400 and “low payment” otherwise.
I keep getting this error
ERROR: DS-00075 : Parsing error occurred while trying to %EVAL an
expression: Invalid syntax found in call to %EVAL**
Can someone tell me what I'm doing wrong?
%LET root=D:\Users\Data;
libname training "&root.";
%LET dataset=training.monthlypayments;
%LET outlib=out;
%LET outfile=monthlypaymentsclassified;
%LET variable=payment;
%IF %EVAL(&VARIABLE.>400) %THEN %DO;
data &outlib..&outfile.;
set &dataset.;
paymenttype="high payment";
run;
%ELSE %DO;
data &outlib..&outfile.;
set &dataset.;
paymenttype="low payment";
run;
%END;
If you want to make a subset of the data based on values of the variables in the data then you need to use normal SAS code and not macro logic statements. If looks like your macro variable just tells you which data step variable to use in your IF statement.
data &outlib..&outfile.;
set &dataset.;
if &variable > 400 then paymenttype="high payment";
else paymenttype="low payment";
run;

SAS conditional logic to execute another sas program based on condition

I have a dataset naming error_table as follows. All the variables are character
Errorno Error Resolution
001 login check
002 datacheck check
I wanted a logic that executes a sas program If the Errorno is not in 001 and 002. Else stop execution and display the error_table.
I tried the following
%macro test();
proc sql;
select trim(Error_No) into: num from error_table;
quit;
%if &num. not in ("001","002") %then %do;
%include "/path/dev/program.sas";
%end;
%else %do;
proc print data = error_table;
run;
%end;
%mend;
%test;
But, it is throwing an error.
Can anyone please correct the logic.
You need to watch out for the case when the SELECT returns zero rows. You should set a default value to the macro variable NUM.
Is your dataset variable numeric or character? Use the TRIMMED or SEPARATED BY clause instead of the TRIM() function to prevent spaces in the macro variable that is generated by the INTO clause.
%let num=NONE;
select Error_No into: num trimmed from error_table;
Remember that to the macro processor everything is a string so don't but quotes around the values you are trying to match unless they are actually part of the value.
%if NOT (&num. in (001,002)) %then %do;
Also to use the IN operator in macro code you need to make sure you have set the MINDELIMITER option.
I would sugest moving condition with error codes to proc sql.
proc sql;
select count(*) into :num_errors
from error_table
where Errorno in ("001", "002");
quit;
Then in macrovariable you have number of errors that are 001 or 002.
Next step is to check macro-condition:
%if &num_errors. > 0 %then %do;
%include "/path/dev/program.sas";
%end;
%else %do;
proc print data = error_table;
run;
%end;
%mend;