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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User
%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

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

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.

Riteshdell
Quartz | Level 8

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

Riteshdell
Quartz | Level 8

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.

Kurt_Bremser
Super User

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.

Astounding
PROC Star

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.

Riteshdell
Quartz | Level 8
have you checked the values, it will give Pune value in all three rows
Kurt_Bremser
Super User
%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
Astounding
PROC Star

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

sas-innovate-2024.png

Today is the last day to save with the early bird rate! Register today for just $695 - $100 off the standard rate.

 

Plus, pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 9 replies
  • 1151 views
  • 0 likes
  • 3 in conversation