Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_updatevarlength.sas
Go to the documentation of this file.
1/**
2 @file mp_updatevarlength.sas
3 @brief Change the length of a variable
4 @details The library is assumed to be assigned. Simple character updates
5 currently supported, numerics are more complicated and will follow.
6
7 data example;
8 a='1';
9 b='12';
10 c='123';
11 run;
12 %mp_updatevarlength(example,a,3)
13 %mp_updatevarlength(example,c,1)
14 proc sql;
15 describe table example;
16
17 @param [in] libds the library.dataset to be modified
18 @param [in] var The variable to modify
19 @param [in] len The new length to apply
20
21 <h4> SAS Macros </h4>
22 @li mf_existds.sas
23 @li mp_abort.sas
24 @li mf_existvar.sas
25 @li mf_getvarlen.sas
26 @li mf_getvartype.sas
27 @li mf_getnobs.sas
28 @li mp_createconstraints.sas
29 @li mp_getconstraints.sas
30 @li mp_deleteconstraints.sas
31
32 @version 9.2
33 @author Allan Bowe
34
35**/
36
37%macro mp_updatevarlength(libds,var,len
38)/*/STORE SOURCE*/;
39
40%if %index(&libds,.)=0 %then %let libds=WORK.&libds;
41
42%mp_abort(iftrue=(%mf_existds(&libds)=0)
43 ,mac=&sysmacroname
44 ,msg=%str(Table &libds not found!)
45)
46
47%mp_abort(iftrue=(%mf_existvar(&libds,&var)=0)
48 ,mac=&sysmacroname
49 ,msg=%str(Variable &var not found on &libds!)
50)
51
52/* not possible to in-place modify a numeric length, to add later */
53%mp_abort(iftrue=(%mf_getvartype(&libds,&var)=0)
54 ,mac=&sysmacroname
55 ,msg=%str(Only character resizings are currently supported)
56)
57
58%local oldlen;
59%let oldlen=%mf_getvarlen(&libds,&var);
60%if &oldlen=&len %then %do;
61 %put &sysmacroname: Old and new lengths (&len) match!;
62 %return;
63%end;
64
65%let libds=%upcase(&libds);
66
67
68data;run;
69%local dsconst; %let dsconst=&syslast;
70%mp_getconstraints(lib=%scan(&libds,1,.),ds=%scan(&libds,2,.),outds=&dsconst)
71
72%mp_abort(iftrue=(&syscc ne 0)
73 ,mac=&sysmacroname
74 ,msg=%str(syscc=&syscc)
75)
76
77%if %mf_getnobs(&dscont)=0 %then %do;
78 /* must use SQL as proc datasets does not support length changes */
79 proc sql;
80 alter table &libds modify &var char(&len);
81 %return;
82%end;
83
84/* we have constraints! */
85
86%mp_deleteconstraints(inds=&dsconst,outds=&dsconst._dropd,execute=YES)
87
88proc sql;
89alter table &libds modify &var char(&len);
90
91%mp_createconstraints(inds=&dsconst,outds=&dsconst._addd,execute=YES)
92
93%mend mp_updatevarlength;