options set=varname "varvalue"; /* set environment variable*/ %put %sysfunc(sysget(varname)); options set=varname="varvalue"; /* set environment variable*/ %put %sysfunc(sysget(varname));
Monday, February 26, 2018
How to set Environment Variable
Tuesday, January 2, 2018
How to use whole ARRAY?
Generally we use ARRAY element (e.g. VARS[1]) just like normal variable in DATA step programming. However, I prefer to using whole array as it can combine the flexibility of array.
Below is listing (update in progress).
#1. ARRAY name only
DIM function
IN operator
FCMP function with VARARGS option (see SAS Sample 41754)
#2. ARRAY name with OF operator (e.g. OF VARS[*])
SUM / MIN / MAX / MEDIAN / MEAN ...
CALL MISSING
CALL SORTN / CALL SORTC
CAT function family (e.g. CAT/CATX/CATS).
...
Below is listing (update in progress).
#1. ARRAY name only
DIM function
IN operator
FCMP function with VARARGS option (see SAS Sample 41754)
#2. ARRAY name with OF operator (e.g. OF VARS[*])
SUM / MIN / MAX / MEDIAN / MEAN ...
CALL MISSING
CALL SORTN / CALL SORTC
CAT function family (e.g. CAT/CATX/CATS).
...
Wednesday, October 18, 2017
Lookup using PROC FCMP data type dictionary
With 9.4M5, we will have one new technique - PROC FCMP dictionary. I wouldn't believe the new data type will be added in DATA STEP/DS2. We have to leverage the new data type via FCMP.
Below is sample code on how to lookup using the new technique.
Below is sample code on how to lookup using the new technique.
proc fcmp outlib=work.funcs.test;
subroutine s(a, b $, c $);
outargs a, b;
declare dictionary d;
if c = 'add' then do;
d[a] = b;
d[b] = a;
end;
else if c = 'get_a' then do;
a = d[b];
end;
else if c = 'get_b' then do;
b = d[a];
end;
endsub;
run;
options cmplib=work.funcs;
data _null_;
a = 1; b='xxx';
call s(a, b, 'add');
call missing(a);
put "Note: get_a before " a= b=;
call s(a, b, 'get_a');
put "Note: get_a after " a= b=;
call missing(b);
put "Note: get_b before " a= b=;
call s(a, b, 'get_b');
put "Note: get_b after " a= b=;
run;
Output:
Note: get_a before a=. b=xxx
Note: get_a after a=1 b=xxx
Note: get_b before a=1 b=
Note: get_b after a=1 b=xxx
Tuesday, October 3, 2017
Easy way to check the file existence
SAS doc suggests "You can use SYSFILRC to confirm that a file or location is allocated before attempting to access an external file". I have not tested if it will work with special file but it works well with regular ones.
16 filename a "c:\"; 17 %put &=sysfilrc; /* file exists. */ SYSFILRC=0 18 19 filename b "c:\_not_exist"; 20 %put &=sysfilrc; /* file does not exist. */ SYSFILRC=1
Saturday, September 23, 2017
Comma position in macro call
Comma is used to separate parameters in the macro call. We seldom care about the comma position.
For example:
Are the two macro calls identical? It may, or may not. If the code is deployed in LATIN1 but runs in UTF-8 session, it will have different result. In first call, the actual value of parameter LIB is "a(invisible carriage return)".
To make a general availability, please always put the comma just behind value in macro call.
For example:
%a(lib=a , name=b) %a(lib=a, name=b)
Are the two macro calls identical? It may, or may not. If the code is deployed in LATIN1 but runs in UTF-8 session, it will have different result. In first call, the actual value of parameter LIB is "a(invisible carriage return)".
To make a general availability, please always put the comma just behind value in macro call.
Wednesday, September 6, 2017
Sample codes for SAS View
SAS has two kinds of views: SQL view and DATA view. They work similarly but we need to know what's the difference. Here, I give out some samples for clarification.
/* create DATA view */
data a / view=a;
set sashelp.class;
run;
/* Create SQL view */
proc sql;
create view b as
select * from sasehlp.class;
quit;
/* Display Data view definition.
Note: it can't be used for SQL view. */
data view=a;
describe;
run;
/* Display SQL view definition.
Note: it can't be used for DATA view. */
proc sql;
describe view b;
quit;
/* There are 4 ways to delete views.
The last one is my favorite as it has most flexibility. */
proc delete data=work.a work.b (memtype=view);
run;
proc sql;
drop view work.a, work.b;
quit;
proc datasets lib=work nolist nowarn;
delete a b / memtype = view;
run; quit;
proc datasets lib=work nolist nowarn memtype=(data view);
delete a b;
run; quit;
Sunday, September 3, 2017
Check if dataset is locked.
Here is the tip to check if the dataset is locked by other PID. It is useful as it has write operation but actually does not change anything.
proc append base=dsn data=dsn(obs=0); run;
Subscribe to:
Posts (Atom)