BookmarkSubscribeRSS Feed
JonathanWarrick
Calcite | Level 5
I have a macro that is being run on several different user IDs, but I'd like to run the macro on the entire population as well. Right now, the macro call uses the following variables:

USER ID
VAR1
VAR2
DATE1
DATE2

I'd like to run the macro on ALL USER IDs, so instead of my macro call being:
%MACRO(132, ABC, DEF, JAN, FEB) what would I do to get it to run on all USER IDs?
6 REPLIES 6
Reeza
Super User
Depends on how you want to call it and what your macro does...

1. Change your macro so that the first parameter is handled as a variable list
2. Create another macro that loops through your list of ids in a dataset and calls the macro.


If you may want to do a few ID's at a time rather than a whole dataset you may want to consider 1 as the most viable option.
Ksharp
Super User
Another Choice is to use call execute() ----My favorite.
Which can generate code dynamically with your dataset.
But firstly you need to transpose this dataset with proc transpose.


Ksharp
Peter_C
Rhodochrosite | Level 12
my toolbag carries a macro I use for situations like this. I even presented it at a SAS conference
"make light work of list processing"
http://www2.sas.com/proceedings/sugi31/012-31.pdf
JonathanWarrick
Calcite | Level 5
Is there a way to set the USER ID to a default value so that every single record has that value (basically making that field irrelevant) so I can run the macro on every single record? I tried using the following code to do this, but I'm getting this error:

%MACRO NAME(NAME,ID);

%MACRO MONTH(month);

data &NAME._&month.;
set all_&month._nores;
%if "&NAME." eq "CONS" %then %do;
USER_ID eq '888888888';
%end;
run;

data &NAME._&month.;
set &NAME._&month.;
if USER_ID eq &ID.;
run;

%MEND MONTH;

%MONTH(&cur_month.);
%MONTH(&prev_month1.);
%MONTH(&prev_month2.);
%MONTH(&prev_month3.);
%MONTH(&prev_month4.);
%MONTH(&prev_month5.);

run;

(it continues for a while, but the rest is unnecessary)


Here are a few of the Macro calls:

%NAME(CONS,'888888888');
%NAME(AH,'904029181');
%NAME(BU,'903769181');


Here is the log/error:

1705 %NAME(CONS,'888888888');
NOTE: Line generated by the invoked macro "MONTH".
3 USER_ID eq '888888888';
-----------------
180
ERROR 180-322: Statement is not valid or it is used out of proper order.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.CONS_APR11 may be incomplete. When this step was stopped there were 0
observations and 82 variables.
NOTE: DATA statement used (Total process time):
real time 0.21 seconds
cpu time 0.01 seconds




NOTE: There were 0 observations read from the data set WORK.CONS_APR11.
NOTE: The data set WORK.CONS_APR11 has 0 observations and 82 variables.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.01 seconds
ballardw
Super User
I think you meant:

%if "&NAME." eq "CONS" %then %do;
USER_ID = '888888888'; /*assignment*/
%end;

instead of
%if "&NAME." eq "CONS" %then %do;
USER_ID eq '888888888'; /*comparison*/
%end;
JonathanWarrick
Calcite | Level 5
Yup, that was the problem! Stupid mistake - thanks for pointing it out!

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1104 views
  • 0 likes
  • 5 in conversation