BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kimmygzc
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;

 

 

--
Paige Miller

View solution in original post

15 REPLIES 15
PaigeMiller
Diamond | Level 26

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
--
Paige Miller
kimmygzc
Obsidian | Level 7

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.

PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
kimmygzc
Obsidian | Level 7

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.

PaigeMiller
Diamond | Level 26

Can't read your code, please reformat it.

--
Paige Miller
kimmygzc
Obsidian | Level 7

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;
 

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
kimmygzc
Obsidian | Level 7

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.

 

PaigeMiller
Diamond | Level 26

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;

 

 

--
Paige Miller
kimmygzc
Obsidian | Level 7

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!

Kurt_Bremser
Super User

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;
kimmygzc
Obsidian | Level 7

Oh, yes! This avoid a transit assignment, Thank you, Sir!

Kurt_Bremser
Super User

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 15 replies
  • 3631 views
  • 0 likes
  • 3 in conversation