BookmarkSubscribeRSS Feed
Nupur20
Calcite | Level 5

Hi,

I am performing a macro with statements :

%macro stuff ();

%do i=2 %to 6  ....... followed by series of statements and then macro end statement.

My question is when I write "%do i=2 %to 6", this mean that macro would perform on colum 2 to column 6 in my data set.

But if I want to skip column 4 and start macro function again from 5, then which statement should I write for "%do i=2 %to 6".

I have tried several different statements like

%do i=2 %to 3 and %do i=5 %to 6 but nothing is working.

Is there a way to do this in same statement or should I have to write it in altogether different syntax?

Thanks a lot for your suggestions.

10 REPLIES 10
art297
Opal | Level 21

I'm not sure if I understand what you are trying to do.  If I do understand you correctly, couldn't you just nest a %if statement in your code to skip the undesired column?  E.g.:

data have;

  input x1-x6;

  cards;

1 1 1 1 1 1

2 2 2 2 2 2

1 2 3 4 5 6

;

 

%macro stuff;

  data want;

    set have;

    %do i=1 %to 6;

      %if &i. ne 4 %then %do;

        x&i=x&i+1;

      %end;

    %end;

  run;

%mend;

 

%stuff

newbie
Calcite | Level 5

Hi,

specify the looping values directly..hope the below one helps.

%do i=2,3,5,6 ;

...

%end;

art297
Opal | Level 21

Newbie, I don't think that the form you suggested will work with a %do loop.

Nupur20
Calcite | Level 5

Hi,

Thanks for your reply.

actually what I want is:

I have Questions 1 to 200 in my dataset as column.

For eg:

Role    Q1      Q2     Q3     Q4      Q5.......Q200

I have to perform a same macro function for Q5 to Q12 and Q100 to Q110. so in

%do i=5 to %to 12 statement, woould ie be possible for me to include Q100 to Q110 as well excluding all questions (i.e Q 13 toQ99) in between.

I am triying the shorten the code by avoiding wrting macro %do stratements again for Q100 to Q110 as I did for Q5 to Q12.

Is that possible?

art297
Opal | Level 21

I'd still recommend nesting a %if statement.  e.g.,

%macro stuff;

  data want;

    set have;

    %do i=5 %to 110;

      %if &i. le 12 or &i. ge 100 %then %do;

        x&i=x&i+1;

      %end;

    %end;

  run;

%mend;

%stuff

Tom
Super User Tom
Super User

Are you sure you want a macro?  If you are performing these operations in a datastep then just make an array and loop of the array.

For example to recode 99 to missing for a select group of variables.

array m99 q5-q12 q100-q112 ;

do i=1 to dim(m99);

   if m99(i)=99 then varlist(i)=.;

end;

Now you cannot do the exact thing in macro language but you can get close by putting you list of variable names in one macro variable and assigning the variable name to another macro variable using a loop counter and the scan function.

%let varlist = q5 q6 q7 q8 q9 q10 q11 q12 q100 q101 q102 q103 q104 q105 q106 q107 q108 q109 q110;

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

    %let var=%scan(&varlist,&i);

   . . .

%end;

Nupur20
Calcite | Level 5

I tried this for different questions and my code looks likethis:

%macro stuff();

%let varlist = Q4 Q6 Q7 Q10 Q13;

%do i=4 %to %sysfunc(countw(&varlist.));

    %let var=%scan(&varlist.,&i.);

proc sql noprint;

create table test&i. as

select role, cats(count(Q&i.),'(',put(count(Q&i.)/count(*),percent8.),')') as C&i.

from data

    group by role;

quit;

%end;

%mend stuff;

%stuff;

Its giving me only 4 test data, test 4 test 5 test 6 and test 7. Also, I want my test data to br named as my selected questions. for eg: with above example the test data should be test 4, test 6, test 7, test 10 and test 13.

Tom
Super User Tom
Super User

Looks like you want to loop over a set of SUFFIXES and use those suffixes for both specifing the name of the output dataset and the variable name.

%macro stuff(suffixes);

%local i j ;

proc sql noprint;


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

%let j=%scan(&suffixes,&i);

  create table test&j as

    select role

         , cats(count(Q&j),'(',put(count(Q&j)/count(*),percent8.),')') as C&j

    from data

    group by role

  ;

%end;


quit;

%mend stuff;

%stuff(4 6 7 10 13);

newbie
Calcite | Level 5

@art297: i overlookd it.thanks for correcting

Ksharp
Super User

Another way is to rename  your variable.

Such as:

proc datasets library=work nolist;

modify temp;

rename Q50-Q60=A1-A10 Q100-Q120=A11-A31;

quit;

Then use your loop macro code.

The macro code looks like my code?

Smiley Happy

Ksharp

Message was edited by: xia keshan

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
  • 10 replies
  • 996 views
  • 0 likes
  • 5 in conversation