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

Hi Everyone,

I want to run my code for different values of variable lookback using this rule
Do lookback=4 to 10 by 2;

I know that I can make a macro say %macro mymacro(lookback=) and then list the value.

This code is for demonstration purpose only.

I really want to make a DO to pass the value into the body of the code.

Could you please help me with that?

Thank you,

HHC

Do lookback=4 to 10 by 2;

data pfm; set pfm;
drop i j l1 h1;
lowest=1000;
highest=0;
i+1;
do j=i to i+ lookback;
set pfm (keep=low high rename =(low=l1 high=h1)) point=j;
if lowest>l1  then lowest=l1;
if highest<h1  then highest=h1;
end;
run;

data pfm; set pfm;
rename
lowest=lowest_lookback  *How can I turn it into number lookback?;
highest=highest_lookback;
run;
....

1 ACCEPTED SOLUTION

Accepted Solutions
hhchenfx
Barite | Level 11

%macro test;

%do oldvar=2 %to 6 %by 2;

data test&oldvar;

set sashelp.class;

run;

%end;

%mend;

%test;

For discrete value

%let oldvar=4 5 20;

%macro test;

%do i=1 %to %sysfunc(countw(&oldvar));

%let newvar=%scan(&oldvar,&i);

data test&newvar;

set sashelp.class;

run;

%end;

%mend;

%test;

Message was edited by: Harry Ng

View solution in original post

2 REPLIES 2
ballardw
Super User

I'm not sure quite what you are attempting to actually accomplish. Are you trying to use the loop to call the two steps with data pfm within the loop?

If you post some example data, as data step code preferably, and what the desired output should be that would help.

Also for test purposes I would think very carefully about using the:

Data pfm; set pfm;

structure. Reusing the same data set before you have the results you need leads to difficulty determining where a re-assignment error occurs.

hhchenfx
Barite | Level 11

%macro test;

%do oldvar=2 %to 6 %by 2;

data test&oldvar;

set sashelp.class;

run;

%end;

%mend;

%test;

For discrete value

%let oldvar=4 5 20;

%macro test;

%do i=1 %to %sysfunc(countw(&oldvar));

%let newvar=%scan(&oldvar,&i);

data test&newvar;

set sashelp.class;

run;

%end;

%mend;

%test;

Message was edited by: Harry Ng

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 455 views
  • 3 likes
  • 2 in conversation