m****r 发帖数: 202 | 1 I have to create a new SAS data set, and all variables in the new one should
be added _30 according to the old one. My plan is using the old one to
export .cvs first, in the .cvs replace original variables to original_30.
Then import .cvs to SAS data set.
Could anyone suggest a easier method to finish this job?
Many thanks | c*****t 发帖数: 1712 | | m****r 发帖数: 202 | 3 Thanks. Could you please give me more detail?
My variable names are AS01d,AS01m,AS01y,AS02a,AS02b,AS02c,AS02_age.
I transposed them to observations, but still don't know how to add "_30" to
them.
Thanks a lot | R*********i 发帖数: 7643 | 4 After transposing the var names are changed to values in a new var (_name_),
you can add _30 at the end of the new var, e.g. name1=compress(_name_)||'_
30'. And then transpose back to the original structure using name1 as the ID
var. | R*********i 发帖数: 7643 | 5 If you only have that limited number of variables as listed above you may
want to use "rename" directly. | m****r 发帖数: 202 | 6 proc contents data=old
out=try(keep=varnum name)
noprint;
run;
data new (keep=newname varnum);
set try;
do i=1 to 1500;
if varnum=(i)
then newname=compress(name)||"_30";
end;
run;
proc transpose data=new
out=aa30d(drop=i _name_);
id newname;
run;
The problem here is some original cha variables have been changed to num
ones. This is not exactly I expect. | A*******s 发帖数: 3942 | 7 %macro add_prefix(lib=, table=, prefix=);
proc sql noprint;
select cats(name, '=', substr(cats(upcase("&prefix"), "_", name), 1, 32)
) into: rename_list separated by " "
from dictionary.columns
where libname=upcase("&lib") and memname=upcase("&table")
;
quit;
data &lib..prfx_&table;
set &lib..&table;
rename &rename_list ;
run;
%mend;
【在 m****r 的大作中提到】 : proc contents data=old : out=try(keep=varnum name) : noprint; : run; : data new (keep=newname varnum); : set try; : do i=1 to 1500; : if varnum=(i) : then newname=compress(name)||"_30"; : end;
| m****r 发帖数: 202 | 8 Dear Actuaries,
Thanks a lot for you programming my question.
I need suffix,but this program gives me prefix.
original
AS01d,AS01m,AS01y,AS02a,AS02b,AS02c,AS02_age.
expected AS01d_30,AS01m_30,AS01y_30,AS02a_30,AS02b_30,AS02c_30,AS02_age_30
. | m****r 发帖数: 202 | 9 %macro add_suffix(lib=, table=, suffix=);
proc sql noprint;
select cats(name, '=',name, "_", substr(cats(upcase("&suffix")), 1, 32)
) into: rename_list separated by " "
from dictionary.columns
where libname=upcase("&lib") and memname=upcase("&table")
;
quit;
data &lib..prfx_&table;
set &lib..&table;
rename &rename_list ;
run;
%mend;
It gives me what I expect. Thanks all. | n*****n 发帖数: 3123 | 10 ft. 敢情你是什么都不会啊
30
【在 m****r 的大作中提到】 : Dear Actuaries, : Thanks a lot for you programming my question. : I need suffix,but this program gives me prefix. : original : AS01d,AS01m,AS01y,AS02a,AS02b,AS02c,AS02_age. : expected AS01d_30,AS01m_30,AS01y_30,AS02a_30,AS02b_30,AS02c_30,AS02_age_30 : .
| A*******s 发帖数: 3942 | 11 u need to understand why and how to use substr function to truncate the
variable names--to control the length up to 32 characters.
32)
【在 m****r 的大作中提到】 : %macro add_suffix(lib=, table=, suffix=); : proc sql noprint; : select cats(name, '=',name, "_", substr(cats(upcase("&suffix")), 1, 32) : ) into: rename_list separated by " " : from dictionary.columns : where libname=upcase("&lib") and memname=upcase("&table") : ; : quit; : data &lib..prfx_&table; : set &lib..&table;
|
|