Hello,
I would like to know how to define macro variables using the array function.
Here is what my code looks like :
data _null_; set have;
array var {*} var_1-var_5;
do i = 1 to dim(var);
call symput ("var_(i)", (var_(i)));
end;
run;
Do you have any suggestions on how I can improve it so that it works?
Thanks
@dera wrote:
Hello,
I would like to know how to define macro variables using the array function.
Here is what my code looks like :
data _null_; set have; array var {*} var_1-var_5; do i = 1 to dim(var); call symput ("var_(i)", (var_(i))); end; run;
Do you have any suggestions on how I can improve it so that it works?
Thanks
Can you tell us what you are trying to do? I can see you probably want to create 5 macro variables but if HAVE has more that one obs you will still only have the values from the last obs of have. Maybe you don't need these macro variables at all. When you start putting a lot of data into macro variables "things" get messy.
@dera wrote:
There are a lot of obs in the HAVE data set so there is a do-loop command right before that.
Maybe something like this but it is messy.
data have;
do id = 1 to 5;
array var_[5];
do i = 1 to dim(var_);
var_[i] = ranuni(1);
end;
output;
end;
run;
data _null_;
set have;
array var_[*] var_1-var_5;
do i = 1 to dim(var_);
call symputx(cats('ID_',id,'_',vname(var_[i])),var_[i],'G');
end;
run;
%put _global_;
239 %put _global_; GLOBAL ID_1_VAR_1 0.1849625698 GLOBAL ID_1_VAR_2 0.9700887157 GLOBAL ID_1_VAR_3 0.3998243061 GLOBAL ID_1_VAR_4 0.2593986454 GLOBAL ID_1_VAR_5 0.9216025779 GLOBAL ID_2_VAR_1 0.9692773498 GLOBAL ID_2_VAR_2 0.5429791731 GLOBAL ID_2_VAR_3 0.5316917228 GLOBAL ID_2_VAR_4 0.0497940262 GLOBAL ID_2_VAR_5 0.0665665516 GLOBAL ID_3_VAR_1 0.8193185706 GLOBAL ID_3_VAR_2 0.5238705215 GLOBAL ID_3_VAR_3 0.8533943109 GLOBAL ID_3_VAR_4 0.0671845768 GLOBAL ID_3_VAR_5 0.9570238576 GLOBAL ID_4_VAR_1 0.2971939642 GLOBAL ID_4_VAR_2 0.2726117891 GLOBAL ID_4_VAR_3 0.6899296309 GLOBAL ID_4_VAR_4 0.9767648624 GLOBAL ID_4_VAR_5 0.2265075185 GLOBAL ID_5_VAR_1 0.6882365503 GLOBAL ID_5_VAR_2 0.4127638663 GLOBAL ID_5_VAR_3 0.5585541127 GLOBAL ID_5_VAR_4 0.2872256107 GLOBAL ID_5_VAR_5 0.475789305 GLOBAL ID_6_VAR_1 0.6882365503 GLOBAL ID_6_VAR_2 0.4127638663 GLOBAL ID_6_VAR_3 0.5585541127 GLOBAL ID_6_VAR_4 0.2872256107 GLOBAL ID_6_VAR_5 0.475789305
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.