DATA Step, Macro, Functions and more

Simplify multiple %let statement

Accepted Solution Solved
Reply
Contributor
Posts: 61
Accepted Solution

Simplify multiple %let statement

Hi, All,

I ran into a situation that I have to assign multiple values to multiple macro variables like the following:

    %let V0 = var_4;

    %let V1 = var_5;

    %let V2 = var_6;

    %let V3 = var_8;

    %let V4 = var_9;

    %let V5 = var_10;

    %let V6 = var_13;

    %let V7 = var_15;

My question is: Is there anyway we can simplify this process to pair up macro values to macro variables, instead of writing 8 %let statements?

Thanks.

Joe


Accepted Solutions
Solution
‎03-17-2015 10:03 AM
Valued Guide
Posts: 858

Re: Simplify multiple %let statement

Not sure if this will suit your purpose but I use this for passwords, depending on how the data is used and how often it changes this might not be a good solution:

data LIB.FILE;

infile cards;

informat pw $13.;

input var1 $ var2 $;

cards;

V0 VAR_4

V1 VAR_5

V2 VAR_6

;

run;

data _null_;set LIB.FILE;

if var1 = 'V0' then call symputx('VAR4',var2);

if var1 = 'V1' then call symputx('VAR5',var2);

if var1 = 'V2' then call symputx('VAR6',var2);

run;

View solution in original post


All Replies
Super User
Posts: 6,939

Re: Simplify multiple %let statement

To automate code like this, you either need a rule for (macro variable number = var_ number), or you need a table (dataset) that contains the relationship.

Is there any way that you can state the relationship in a formula?

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

Re: Simplify multiple %let statement

There is no formula for the relationship. Different variables (var_ ) are picked for different runs to feed the V0-V7, which were used in a following macro.

What I can do it to put them together in a data set like this:

data have;

input target$ feed$;

cards;

V0 var_4

V1 var_5

V2 var_6

V3 var_8

V4 var_9

V5 var_10

V6 var_13

V7 var_15

;

Solution
‎03-17-2015 10:03 AM
Valued Guide
Posts: 858

Re: Simplify multiple %let statement

Not sure if this will suit your purpose but I use this for passwords, depending on how the data is used and how often it changes this might not be a good solution:

data LIB.FILE;

infile cards;

informat pw $13.;

input var1 $ var2 $;

cards;

V0 VAR_4

V1 VAR_5

V2 VAR_6

;

run;

data _null_;set LIB.FILE;

if var1 = 'V0' then call symputx('VAR4',var2);

if var1 = 'V1' then call symputx('VAR5',var2);

if var1 = 'V2' then call symputx('VAR6',var2);

run;

Contributor
Posts: 61

Re: Simplify multiple %let statement

thanks.

I worked it out in a similar fashin.

DATA _NULL_;

SET have;

CALL SYMPUT(target, feed);

RUN;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 498 views
  • 1 like
  • 3 in conversation