Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mf_getvalue.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Retrieves a value from a dataset. Returned value is fetched from the
4 'fetchobs=' record (row 1 by default), after applying the optional filter.
5 Returns a vartype-specific missing value if no row is returned.
6
7 @details Be sure to <code>%quote()</code> your where clause. Example usage:
8
9 %put %mf_getvalue(sashelp.class,name,filter=%quote(age=15));
10 %put %mf_getvalue(sashelp.class,name);
11
12 <h4> SAS Macros </h4>
13 @li mf_getvartype.sas
14
15 <h4> Related Macros </h4>
16 @li mp_setkeyvalue.sas
17
18 @param [in] libds dataset to query
19 @param [in] variable the variable which contains the value to return.
20 @param [in] filter= (1) contents of where clause
21 @param [in] fetchobs= (1) observation to fetch. NB: Filter applies first.
22 @param [in] warn= (0) Set to 1 to raise a w@rning log message on attempt to
23 read past end of the input dataset.
24
25
26 @version 9.2
27 @author Allan Bowe
28**/
29
30%macro mf_getvalue(libds,variable,filter=1,fetchobs=1,warn=0
31)/*/STORE SOURCE*/;
32 %local dsid rc vartype result;
33
34 %let vartype = %mf_getvartype(&libds,&variable);
35
36 %let dsid=%sysfunc(open(&libds(where=(&filter))));
37 %if (&dsid) %then %do;
38 %let rc = %sysfunc(fetchobs(&dsid,&fetchobs));
39 %if (&rc ne 0) %then %do;
40 /* Fetchobs() failed - Log any issue that was not 'read past end' */
41 %if (&rc ne -1) %then %put %sysfunc(sysmsg());
42 /* Set the result to a missing value dependent on type. */
43 %if (&vartype eq N) %then %let result = .;
44 %else %let result = ;
45
46 %if (&warn) %then %do;
47 %if (&rc eq -1) %then %do; /* read past end of data */
48 %put %sysfunc(
49 compbl(
50 %quote(WARN%str(ING): Missing value returned for variable)
51 %quote(&variable after reading past end of &libds..)
52 )
53 );
54 %let rc=4;
55 %end;
56 %end;
57 /* And update SYSCC if the &rc value is higher */
58 %let syscc = %sysfunc(max(&syscc,&rc));
59 %end;
60 %else %do;
61 %let result = %sysfunc(
62 getvar&vartype(
63 &dsid
64 ,%sysfunc(varnum(&dsid,&variable))
65 )
66 );
67 %end;
68 %let rc = %sysfunc(close(&dsid));
69
70 %trim(&result)
71
72 %end;
73 %else %do;
74 %put %sysfunc(sysmsg());
75 %let syscc = %sysfunc(max(&syscc,%sysfunc(sysrc())));
76 %end;
77
78%mend mf_getvalue;