DATA Step, Macro, Functions and more

macro with array

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

macro with array

Dear SAS Community,

I am trying to use a macro within an array statement. The first set of commands worked, but the second didn't. I'd appreciate your help.

Thanks,

Brent Fulton

UC Berkeley

THIS WORKED

data new;

set old;

array a {*} $ proc1 proc2 proc3;

array b {*} $ proc1b proc2b proc3b;

do j=1 to dim(a);

b{j} = substr(a{j},1,1);

end;

run;

THIS DIDN'T WORK (it is same as above, except use macro proclist. error was "Expecting a variable length specification.")

%let proclist='proc1 proc2 proc3';

data new;

set old;

array a {*} $ &proclist;

array b {*} $ proc1b proc2b proc3b;

do j=1 to dim(a);

b{j} = substr(a{j},1,1);

end;

run;


Accepted Solutions
Solution
‎10-23-2011 05:54 AM
Trusted Advisor
Posts: 1,245

macro with array

Hi There,

In your %let statement remove the single quotes then it will work.

%let proclist=proc1 proc2 proc3;

The reason being is because the macro facility is text and as such you don't need to quote your macro variable values.

Cheers,

Michelle

View solution in original post


All Replies
Solution
‎10-23-2011 05:54 AM
Trusted Advisor
Posts: 1,245

macro with array

Hi There,

In your %let statement remove the single quotes then it will work.

%let proclist=proc1 proc2 proc3;

The reason being is because the macro facility is text and as such you don't need to quote your macro variable values.

Cheers,

Michelle

Occasional Contributor
Posts: 14

macro with array

Thank you, Michelle, that worked.

Btw, is there a way to have a macro contain a numeric?

And as follow-up to arrays, I am trying to do the following: (1) change missings to 'zzzzz', and then add leading zeros e.g., make '123' be '00123'. All variables are character length 5. However, I get an invalid argument to the input function below. When I don't use arrays, it works fine. I'd appreciate your advice.

Last, I'm running out of disk space. Is there a way to do this without creating two additional sets of variables (i.e., proclistb and proclistc)?

Thanks,

Brent

%let proclist proc1 proc2 proc3;

%let proclistb=proc1b proc2b proc3b;

%let proclistc=proc1c proc2c proc3c;

run;

*change missings to zzzzz, add leading zeros;

data new;

set old;

array a {*} $5 &proclist;

array b {*} $5 &proclistb;

array c {*} $5 &proclistc;

do j=1 to dim(a);

if a{j}~=' ' then b{j} = a{j}; else b{j}=zzzzz; /* do i need to have it be 'zzzzz' ?*/

c{j} = put(input(b{j},best.),z5.);

end;

run;

Super User
Super User
Posts: 6,495

macro with array

You need enclose string literals inside of quotes.  Without the quotes SAS will look for a variable with the name zzzzz.

There you are not dealing with macro code at all, just normal SAS syntax.

Occasional Contributor
Posts: 14

macro with array

Thank you, Tom. I switched the line in question above to:

if a{j}~=' ' then b{j} = a{j}; else b{j}='zzzzz';

However, I still get an invalid argument error to the input function on the next line:

c{j} = put(input(b{j},best.),z5.);

Do you or others have suggestions?

thanks,

Brent

Valued Guide
Posts: 765

Re: macro with array

Hi ... if it's just the PROC variables you want to edit , you can skip the macro variable (yes/no?).

If you just want to vary the number of PROCs, use that number as the macro variable.

data old;

input (proc1 proc2 proc3) (: $5.);

datalines;

12345 . 999

88 77 .

;

run;

%let n=3;

*change missings to zzzzz, add leading zeros;

data nw;

set old;

array proc{&n};

do j=1 to &n;

   proc{j} = ifc(^missing(proc(j)), translate(right(proc(j)),'0',' '), 'zzzzz');

end;

drop j;

run;


proc1    proc2    proc3

12345    zzzzz    00999

00088    00077    zzzzz


Occasional Contributor
Posts: 14

macro with array

this worked well, Mike, thank you.

best,

Brent

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 172 views
  • 6 likes
  • 4 in conversation