data classivar_1;
set classvar;
AnaClassVar=scan(scan(F2,1," "),2,".");
run;
proc sql;
select AnaClassVar into : MacClassVar separated by "#" from classivar_1 ;
select count(*) into: Count_classvar from classivar_1;
quit;
%put &MacClassVar.;
%put &Count_classvar.;
ods output variables=adsl_var;
proc contents data=ev.adsl;
run;
proc sql;
select variable into : AllVar separated by "#"
from adsl_var;
select count(*) into : Count_Allvar from adsl_var;
quit;
%put &Allvar.;
%put &Count_Allvar.;
**** set up Macro ClassAna to analyze the classified varialbes;
%macro ClassAna(datasets= );
%do i= 1 %to &Count_classvar.;
%do count=1 %to &Count_Allvar;
%if %sysfunc(find(%scan(&MacClassVar,&i,#),%scan(&AllVar,&count,#)))
%then %do;
%let Class_var&i.=%scan(&AllVar,&count,#);
%end;
%end;
%put &&Class_var&i..;
%end;
%Mend;
%ClassAna(datasets=sashelp.class)
When I submit the programme , the macro variable Class_var6 cannot be resolved.
But other macro variables can be resolved correctly.
The logs are in the picture.enter image description here
enter image description here
In %ClassAna you are conditionally creating the macro vars based on:
%if %sysfunc(find(%scan(&MacClassVar,&i,#),%scan(&AllVar,&count,#)))
%then %do;
That FIND is case sensitive by default. I think it will work if you make it case insensitive by adding the optional i parameter to FIND. Something like:
%if %sysfunc(find(%scan(&MacClassVar,&i,#),%scan(&AllVar,&count,#),i))
%then %do;
Or you could %upcase both variable lists and leave the FIND as is.
Related
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
I'm pretty new to doing macros on SAS so apologies in advance if my questions seems basic.
I'm looking to execute part of a macro if there are a minimum number of observations found in temporary work file.
If there are a minimum number of observations, the macro should execute a proc export of that temporary work file, otherwise it will just produce an error message on the log.
I'm not sure what I'm doing wrong, the temporary work file has more than 1 observation and I want the macro to run, however it produces the error message as if it has less than the minimum.
Below is the code that I've created so far - any help you have to offer will be greatly welcomed.
data original_data;
set data.dataset;
keep column1 column2 column3 ;
run;
%macro test_macro;
%let dsid=%sysfunc(open(work.original_data));
%let nobs=%sysfunc(attrn(&dsid,nobs));
%let dsid=%sysfunc(close(&dsid));
%if &nobs >1 %then %do;
%put ERROR: No observations counted;
%return
%end
%else %do;
proc export data=submissions
outfile='C:\Users\myusername\Desktop\test.csv'
DBMS=csv replace;
run;
%end;
%mend test_macro;
%test_macro
Missing semicolons on RETURN and END statements
Logic is reversed. You're putting the error message if you have more than one observation. Flip your code so that you export if you have more than one observation.
options mprint symbolgen;
%macro test_macro;
%let dsid=%sysfunc(open(work.original_data));
%let nobs=%sysfunc(attrn(&dsid, nobs));
%let dsid=%sysfunc(close(&dsid));
%put &nobs;
*if more than one observation;
%if &nobs > 1 %then
%do;
proc export data=sashelp.class outfile='/home/fkhurshed/test.csv' DBMS=csv
replace;
run;
%end;
%else
%do;
%put ERROR: No observations counted;
%return;
%end;
%mend test_macro;
%test_macro;
I am trying to run a macro based on a condition of usernames, where do I make the changes:
for ex:
I have the following dataset with usernames:
data users2;
input name$;
cards;
ABC
DEF
YUT
GTR
;
run;
I have a macro to call: %callmacro;
proc sql;
select name into: usernames separated by ',' from users2;
quit;
so I call the macro
%macro NEW();
%if &sysuserid in (&usernames) %then %do;
%callmacro;
%end;
%mend;
%new;
So here I get an error :
ERROR: Required operator not found in expression: ( "&sysuserid" in
(&usernames))
I would like to run a macro only if the username matches in the list. Else is there any way I can call a WINDOWS AD group from SAS macro and check if the sysuserid exixts in that Windows AD group?
You could check the usernames inside the macro
%macro ThisIsConditionallyRestricted(nametable=users2);
proc sql noprint;
select name from &nametable where name = "&sysuserid";
quit;
%if &SQLOBS = 0 %then %do;
%put WARNING: You were not prepared!;
%return;
%end;
…
%mend;
%ThisIsConditionallyRestricted;
You can't use in statement in %if clause without special option. There are two ways to solve the problem.
1. /minoperator option(you need change delimeter in &usernames from ',' to ' '. ):
Firstly, you need to change delimeter in usernames macro variable and use strip function:
proc sql;
select strip(name) into: usernames separated by ' ' from users2;
quit;
Then, your code with option /minoperator.
%macro new() /minoperator;
%if &sysuserid in (&usernames) %then %do;
%callmacro;
%end;
%mend;
%new;
2. The other solution is to use loop by scan function(no need in changing delimeter):
%macro new();
%do i = 1 %to %sysfunc(countw(%bquote(&usernames),%str(%,)));
%if %sysfunc(strip(%str(&sysuserid)))=%sysfunc(strip(%scan(%bquote(&usernames),&i,%str(%,)))) %then %do;
%callmacro;
%end;
%end;
%mend new;
%new();
Don't forget to use strip function, when you compare character
variables and select into. My advise is to change it:
proc sql;
select strip(name) into: usernames separated by ',' from users2;
quit;
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;
Here is the code I'm using to creat a format.....
libname myfmt "&FBRMrootPath./Formats";
%macro Create_Macro(DSN,Label,Start,fmtname,type);
options mprint mlogic symbolgen;
%If &type='n' %then %do;
proc sort data=&DSN out=Out; by &Label;run;
Data ctrl;
set Out(rename=(&Label=label &Start=start )) end=last;
retain fmtname &fmtname type &type;
%If last %then %do;
hlo='O';
label='*ERROR';
output;
%End;
%End;
%Else %do;
proc sort data=&DSN out=Out; by &Start;run;
Data ctrl;
set Out(rename=(&Start=label &Label=start )) end=last;
retain fmtname &fmtname type &type;
output;
%If last %then %do;
hlo='O';
label='*ERROR';
output;
%End;
%End;
proc format library=myfmt cntlin=ctrl;
%Mend Create_Macro;
%Create_Macro(SSIN.prd,prd_nm,prd_id,'prd_test','n');
/*%Create_Macro(SSIN.prd,prd_id,prd_nm,'prd_testc','c');*/
I'm getting following errors...Code looks good but I donno why I'm getting errors...
Any help???
Not entirely sure what you are doing, but the error message is probably because you are mixing macro code with data step code. Trying change to this:
if last then do;
hlo='O';
label='*ERROR';
output;
end;
In other words, get rid of the ampersands (which indicate macro variable references).
And also be sure to add a run; statement at the end of each data step and after the PROC FORMAT call.