Hello,
I am using arrays to transform numeric variables in a SAS data set. I am seeking a quick way to declare transformed variables in arrays, such that a prefix is applied to the original variable name. In the below code, is there any way to declare the variables in the arrays t1var and t2var other than typing them out? I don't want to use t1-3, this would be faster but when my data set has lots of variables it is important that they retain their original names (plus the prefix) so that they can be easily identified.
Data data.Recoded_Variables;
Set data.Variables;
/*data.Variables contains 3 numeric
variables: sales, expenses, employees */
Array var
Array t1var
Array t2var
Do i=1 to dim(var);
t1var= var/lag(var)-1;
t2var= lag(t1var);
End;
Drop i;
Run;
Thank you!
Will
try this one:
data variables;
input id sales expenses employees ;
cards;
1 200 100 2
2 300 200 3
3 400 220 4
;
proc sql noprint;
select name
,cats('t1',name)
,cats('t2',name)
into :name separated by ' '
, :t1name separated by ' '
, :t2name separated by ' '
from dictionary.columns
where libname='WORK' and memname='VARIABLES' and upcase(name) ne 'ID';/* note: libname and memname have to be capital letters */
quit;
data recoded_variables;
set variables;
Array var
Array t1var
Array t2var
Do i=1 to dim(var);
t1var= var/lag(var)-1;
t2var= lag(t1var);
End;
Drop i;
Run;
proc print;run;
Linlin
Message was edited by:Linlin
This should work for any reasonable number of variables with names that arent too long for it to work properly.
data variables;
input sales expenses employees ;
cards;
200 100 2
300 200 3
400 220 4
;;;;
run;
%let vars=Sales-numeric-employees;
proc transpose data=variables(obs=0) out=names;
var &vars;
run;
proc sql noprint;
select _name_ into :vars separated by ' '
from names;
quit;
run;
data names;
length vname $32;
set names;
do vname = _name_,cats('T1',_name_),cats('T2',_name_);
output;
end;
retain n 0;
run;
proc transpose data=names out=arrays(drop=_name_);
var n;
id vname;
run;
data want;
if 0 then set arrays;
Array _v
Array _t1
Array _t2
set variables;
Do i=1 to dim(_v);
_t1= _v/lag(_v)-1;
_t2= lag(_t1);
end;
Drop i;
Run;
proc contents varnum;
proc print;
run;
# Variable Type Len
1 sales Num 8
2 T1sales Num 8
3 T2sales Num 8
4 expenses Num 8
5 T1expenses Num 8
6 T2expenses Num 8
7 employees Num 8
8 T1employees Num 8
9 T2employees Num 8
Obs sales T1sales T2sales expenses T1expenses T2expenses employees T1employees T2employees
1 200 . . 100 . . 2 . .
2 300 0.50000 . 200 1.0 . 3 0.50000 .
3 400 0.33333 0.5 220 0.1 1 4 0.33333 0.5
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.