Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mf_getvarformat.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Returns the format of a variable
4 @details Uses varfmt function to identify the format of a particular variable.
5 Usage:
6
7 data test;
8 format str1 $1. num1 datetime19.;
9 str2='hello mum!'; num2=666;
10 stop;
11 run;
12 %put %mf_getVarFormat(test,str1);
13 %put %mf_getVarFormat(work.test,num1);
14 %put %mf_getVarFormat(test,str2,force=1);
15 %put %mf_getVarFormat(work.test,num2,force=1);
16 %put %mf_getVarFormat(test,renegade);
17
18 returns:
19
20 $1.
21 DATETIME19.
22 $10.
23 8.
24 NOTE: Variable renegade does not exist in test
25
26 @param [in] libds Two part dataset (or view) reference.
27 @param [in] var Variable name for which a format should be returned
28 @param [in] force= (0) Set to 1 to supply a default if the variable has no
29 format
30 @returns outputs format
31
32 @author Allan Bowe
33 @version 9.2
34**/
35
36%macro mf_getVarFormat(libds /* two level ds name */
37 , var /* variable name from which to return the format */
38 , force=0
39)/*/STORE SOURCE*/;
40 %local dsid vnum vformat rc vlen vtype;
41 /* Open dataset */
42 %let dsid = %sysfunc(open(&libds));
43 %if &dsid > 0 %then %do;
44 /* Get variable number */
45 %let vnum = %sysfunc(varnum(&dsid, &var));
46 /* Get variable format */
47 %if(&vnum > 0) %then %let vformat=%sysfunc(varfmt(&dsid, &vnum));
48 %else %do;
49 %put NOTE: Variable &var does not exist in &libds;
50 %let rc = %sysfunc(close(&dsid));
51 %return;
52 %end;
53 %end;
54 %else %do;
55 %put &sysmacroname: dataset &libds not opened! (rc=&dsid);
56 %put &sysmacroname: %sysfunc(sysmsg());
57 %return;
58 %end;
59
60 /* supply a default if no format available */
61 %if %length(&vformat)<2 & &force=1 %then %do;
62 %let vlen = %sysfunc(varlen(&dsid, &vnum));
63 %let vtype = %sysfunc(vartype(&dsid, &vnum.));
64 %if &vtype=C %then %let vformat=$&vlen..;
65 %else %let vformat=best.;
66 %end;
67
68
69 /* Close dataset */
70 %let rc = %sysfunc(close(&dsid));
71 /* Return variable format */
72 &vformat
73%mend mf_getVarFormat;