DATA Step, Macro, Functions and more

reading value from macro varible

Accepted Solution Solved
Reply
Contributor
Posts: 68
Accepted Solution

reading value from macro varible

hi Expert,

 

I need a small help to get macro value in to data set.

 

I have 3 macro variable:

% let 1='Pune';

%let 2='mumbai';

% let 3='chennai';

 

and I have a dataset have which has a varibale name A;

data have;

input a$;

datalines;

1

2

3

;

run;

 

I want a output with another variable with assiciated marco variable value, A value should call to macro varible and get macro variable value.

 

Ouput :

A        B

1       Pune

2      Mumbai

3       Chennai


Accepted Solutions
Solution
‎07-13-2017 09:14 AM
Super User
Posts: 7,863

Re: reading value from macro varible

Posted in reply to Ritesh_dellvostro
%let v1=Pune;
%let v2=umbai;
%let v3=chennai;

data have;
input a$;
datalines;
v1
v2
v3
;
run;

data want;
set have;
b = symget(a);
run;

proc print data=want noobs;
run;

Result:

a     b

v1    Pune   
v2    umbai  
v3    chennai
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 7,863

Re: reading value from macro varible

Posted in reply to Ritesh_dellvostro

Single digits cannot be used as macro variable names. Macro variable names can only contain letters, digits and underlines, and must not start with a digit.

 

For your task, use a value format:

proc format library=work;
value $indian_cities
  '1' = 'Pune'
  '2' = 'mumbai'
  '3' = 'chennai'
;
run;

data want;
set have;
b = put(a,$indian_cities.);
run;
  

No macro programming needed at all.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 68

Re: reading value from macro varible

Posted in reply to KurtBremser

Thanks !

 

Suppose if we would have macro varibale name a,b,c instead of 1,2,3.

 

Then how we can get result by doing macro Only.?

Super User
Posts: 7,863

Re: reading value from macro varible

Posted in reply to Ritesh_dellvostro

You're working with data. The macro language is designed for code creation, not data handling. Forget it.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Contributor
Posts: 68

Re: reading value from macro varible

Posted in reply to KurtBremser

If I have thousand value, then this will be more Hard code values by using proc format.

 

Is it poosible by doing macro.? when it will check the col1 and varible name and assign the value in to col2.

Super User
Posts: 7,863

Re: reading value from macro varible

Posted in reply to Ritesh_dellvostro

Handling a thousand macro variables is inherently more work than a proc format and would be INCREDIBLY stupid.

 

Keep your thousand values in a dataset (where they belong), and run proc format off that.

A data step that has your values in a cards; section is MUCH easier to write and maintain than a thousand %lets.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,518

Re: reading value from macro varible

Posted in reply to Ritesh_dellvostro

As you have already heard, there may be better ways to handle this.  But if this is your starting point, it is possible:

 

% let v1=Pune;

%let v2=umbai;

% let v3=chennai;

 

data have;

input a$;

datalines;

v1

v2

v3

;

 

 

You can retrieve macro variables based on the value of A in this way:

 

data want;

set have;

b = symget(a);

run;

 

By default, B will have a length of $200.  You can always change that with a LENGTH statement after the SET statement.

 

Also note that quotes are not advisable when %LET assigns values to macro variables.

Contributor
Posts: 68

Re: reading value from macro varible

Posted in reply to Astounding
have you checked the values, it will give Pune value in all three rows
Solution
‎07-13-2017 09:14 AM
Super User
Posts: 7,863

Re: reading value from macro varible

Posted in reply to Ritesh_dellvostro
%let v1=Pune;
%let v2=umbai;
%let v3=chennai;

data have;
input a$;
datalines;
v1
v2
v3
;
run;

data want;
set have;
b = symget(a);
run;

proc print data=want noobs;
run;

Result:

a     b

v1    Pune   
v2    umbai  
v3    chennai
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 5,518

Re: reading value from macro varible

Posted in reply to Ritesh_dellvostro

The code works fine, as long as you remove the space after the %.  %LET with no space after the % sign.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 168 views
  • 0 likes
  • 3 in conversation