I'm doing a batch work with SAS programming, where I have a series of var to assign.
with vars = va|vb|vc
, in a do loop
, get var = vars[i],
, get temp local var_i
, let &var(actually va/vb/vc) = &&var_&i;
But the last step isn't working, I guess SAS won't let variable resolved at [LET VAR_NM = ] syntax..
So, Is there a function I may use to copy value from variable to variable?
Or any better way to finish this batch work?
Thanks very much, Say hi to the community from Guangzhou, China.
First, you almost never want macro variable values to be enclosed in quotes. That's just not necessary.
Next, you say "I get t = 'Hi, 1' in the %get_value function." Does this mean that macro variable &t is assigned the value of 'Hi, 1'? Because the way you wrote it when you referred to t, it seems like it is not a macro variable.
Since I still don't know what %get_value does, perhaps adding in
%global t;
right at the top of the code will allow &t to pass its value into your command
%let &var=&t;
I'm not 100% sure I understand what you are asking here ... but maybe this is it ...
%macro dothis;
%let vars=va vb vc;
%do i=1 %to %sysfunc(countw(&vars));
%let var&i=%scan(&vars,&i,%str( ));
%end;
%put &var1 &var2 &var3;
%mend;
%dothis
Thanks for your reply, Sir. This is partial of my work though, once you get var1 which is the name of my wanted global vars, va respectively. I need to convey a local value from another temp var. I did [%let &&var&i = &var_temp] in your context, hoping this would make va = &var_temp globally, but It failed...
Thank you for your patience again, Sir.
@kimmygzc wrote:
Thanks for your reply, Sir. This is partial of my work though, once you get var1 which is the name of my wanted global vars, va respectively. I need to convey a local value from another temp var. I did [%let &&var&i = &var_temp] in your context, hoping this would make va = &var_temp globally, but It failed...
Thank you for your patience again, Sir.
I guess then I don't understand what you are trying to do, and it would help if you can be more specific and give concrete examples of "I need to convey a local value from another temp var". I'm not asking for code, I'm asking what the macro variable values are how you want to move them around.
Sure, Say:
%let vars = va|vb|vc
%macro batch_assign;
%do i = 1 to %sysfunc(countw(&vars, %str(|)));
var = %scan(&vars, &i, %str(|));
%get_value(ind = i, tmep = t); /*In this func, t is assigned with 'Hi,1'/'Hi,2'/'Hi,3'*/
%let &&var = &t; /*var should be twice resolved, which I think SAS would avoid.*/
%mend;
%batch_assign;
What I want is, globally, I have va = 'Hi,1', vb = 'Hi,2', vc = 'Hi,3'; Which is actually, batch assignment from function get_value.
Can't read your code, please reformat it.
Sorry, Sir, Im new to this editor...
%let vars = va|vb|vc; %macro batch_assign; %do i = 1 to %sysfunc(countw(&vars, %str(|))); var = %scan(&vars, &i, %str(|)); %get_value(ind = i, tmep = t); /*In this func, t is assigned with 'Hi,1'/'Hi,2'/'Hi,3'*/ %let &var = &t; /*var should be resolved and refered to va/vb/vc , which I think SAS would avoid.*/ %mend; %batch_assign;
In case if it's not formatted in the block:
%let vars = va|vb|vc;
%macro batch_assign;
%do i = 1 to %sysfunc(countw(&vars, %str(|)));
var = %scan(&vars, &i, %str(|));
%get_value(ind = i, tmep = t); /*In this func, t is assigned with 'Hi,1'/'Hi,2'/'Hi,3'*/
%let &var = &t;
/*var should be resolved and refered to va/vb/vc
, which I think SAS would avoid.*/
%mend;
%batch_assign;
Your code has a wrong %DO statement (no %TO) and misses a %END to close the %DO.
Earlier, I asked for a concrete example showing how values are assigned to the different macro variables. I specifically said I do not want to see code. I don't know what %get_value does, so this isn't helping.
What I can see is this:
&i=1
then &var is assigned the value va.
What happens next (without writing code, what is the next thing that happens)? Then keep going through the entire example.
Yeah, When &i = 1, var = va, I get t = 'Hi, 1' in the %get_value function.
Then I do this in do macro %batch_assign;
%let &var = &t;
I want globally va = 'Hi, 1'.
My SAS is on the company cloud environment, can't just copy code from there. I'm pretty sorry to make it looks complex here. Thanks, Sir.
First, you almost never want macro variable values to be enclosed in quotes. That's just not necessary.
Next, you say "I get t = 'Hi, 1' in the %get_value function." Does this mean that macro variable &t is assigned the value of 'Hi, 1'? Because the way you wrote it when you referred to t, it seems like it is not a macro variable.
Since I still don't know what %get_value does, perhaps adding in
%global t;
right at the top of the code will allow &t to pass its value into your command
%let &var=&t;
Thanks,Sir. Guess I know what's going on here.
Since I %let &var = &t; in the Macro %batch_assign, I was thinking SAS won't allow %let resolve a variable, All I need is just state &var global before I pass in values(coz generally if you use %let globally, it's OK not to make a statement, it doesn't hit me until you say so)...
It's solved now, Thank you very much!
Is this what you want?
%let vars = va|vb|vc;
%macro assign;
%do i = 1 %to %sysfunc(countw(&vars.,|));
%global %scan(&vars,&i,|);
%let %scan(&vars,&i,|) = Hi, &i.;
%end;
%mend;
%assign
%put &=va &=vb &=vc;
Oh, yes! This avoid a transit assignment, Thank you, Sir!
Alternatively, you can do it in a DATA step:
data _null_;
vars = "&vars";
do i = 1 to countw(vars,"|");
call symputx(scan(vars,i,"|"),catx(" ","Hi,",i),"g");
end;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.