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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
MichelleHomes
Meteorite | Level 14

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

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com

View solution in original post

6 REPLIES 6
MichelleHomes
Meteorite | Level 14

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

//Contact me to learn how Metacoda software can help keep your SAS platform secure - https://www.metacoda.com
BrentFulton
Calcite | Level 5

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;

Tom
Super User Tom
Super User

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.

BrentFulton
Calcite | Level 5

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

MikeZdeb
Rhodochrosite | Level 12

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


BrentFulton
Calcite | Level 5

this worked well, Mike, thank you.

best,

Brent

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

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
  • 6 replies
  • 1207 views
  • 6 likes
  • 4 in conversation