proc contents data=sashelp.class noprint out=s(keep=name type) ;run;
proc sort data=s;by type ;run;
proc sql ;
select strip(name)||'1',name into:ne1-:ne5,:n1-:n5 from s ;
quit;
%put &n1 &n2 &n3 &n4 &n5 &ne1 &ne2 &ne3 &ne4 &ne5;
%macro copy1;
%do i=1 %to 5 ;
data ex1 ;
set sashelp.class ;
&&ne&i.=&&n&i.;
run;
%end;
%mend;
%copy1 ;
i want to create 5 new variables like age1,name1,sex1,height1,weight1 so i wrote one applications but i am getting only one variable
what is the issue in that application
Maybe
%macro copy1;
data ex1 ;
set sashelp.class ;
%do i=1 %to 5 ;
&&ne&i.=&&n&i.;
%end;
run;
%mend;
You already got a good answer from @ChrisNZ, but it is much simpler to do this using just SQL and no macro:
proc sql noprint;
select cats(name,'1=',name) into :doit separated by ';' from dictionary.columns
where libname='SASHELP' and memname='CLASS';
quit;
data ex1;
set sashelp.class;
&doit;
run;
Of course, if you are trying to learn macro programming, the other way is a valid exercise. But one of the important things to know about macro language is when NOT to use it.
@s_lassen You still use the "proc sql creates macro variable" method.
But yes, I agree, it's better to build the whole expression as one macro variable.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.