DATA Step, Macro, Functions and more

looping through variables in comma separated list to create datasets

Reply
Regular Contributor
Posts: 235

looping through variables in comma separated list to create datasets

i have a stored process which gets id's

it looks like 17,18,19 for example

now what i want to do here is to append them all to the same dataset but with different variable names (A_17, A_18 , B_17, B_18 )

but at this point it already fails to read my do while statement

any help?

%macro CreateGraphData;
%let i = 0;
%do %while (scan(&curve_id,&i,',') NE '');
current_id = scan(&curve_id,&i,',');

data wacom_&i;
set RF300l3.wacom_main;
where curve_id = current_id;
run;

data wacom_&i (drop = A B);
set RF300l3.wacom_data;
where curve_id = current_id;

A_&i = A;

B_&i = B;
run;

if &i = 1 then do;
data all_data;
set wacom_&i;
run;
end;
else do;
proc append base=all_data data=wacom_&i force;run;
end;

%let i = &i + 1;
%end;
%mend;
%CreateGraphData;

Regular Contributor
Posts: 235

looping through variables in comma separated list to create datasets

ok, i almost there Smiley Happy

%macro GraphsData;
%let num=1;
   %let Curve=%scan(&curve_id,&num,'#');
   %do %while(&Curve ne);
      %put 1: &Curve;
   /* do action for this curve */
data wacom_#
  set RF300l3.wacom_main;
  where curve_id = &Curve;
run;

data wacom_#
  set RF300l3.wacom_data;
  where curve_id = &Curve;
run;
/*
if &i = 1 then do;
  data all_data;
   set wacom_&i;
  run;
end;
else do;
   proc append base=all_data data=wacom_&i force;run;
end;
*/
   /* scan for next round */
      %let num=%eval(&num+1);
      %let Curve=%scan(&curve_id,&num,'#');
   %end;
%mend;
%GraphsData;

Valued Guide
Posts: 2,177

looping through variables in comma separated list to create datasets

you may not have noticed from examples that the macro language does not use quotes to indicate strings, but uses the %STR() function

So, not '#' but %STR(#) might help your %while()

Occasional Contributor
Posts: 6

looping through variables in comma separated list to create datasets

This problem has been solved ?

Can you give an example ?

This isn´t hard, I just don´t get what you need.

Kassim

Super User
Super User
Posts: 7,039

looping through variables in comma separated list to create datasets

Do you need the intermediate datasets with the numeric suffix?

If not then why not just use a data step?  Why do you need a macro?

data all_data;
  set RF300l3.wacom_main;
  where curve_id in (&Curve_id);
run;

Ask a Question
Discussion stats
  • 4 replies
  • 682 views
  • 0 likes
  • 4 in conversation