BookmarkSubscribeRSS Feed
thanikondharish
Fluorite | Level 6

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

5 REPLIES 5
thanikondharish
Fluorite | Level 6
then how to rectify that issue can u help me?
ChrisNZ
Tourmaline | Level 20

Maybe

%macro copy1;
data ex1 ;
set sashelp.class ;
%do i=1 %to 5 ;
&&ne&i.=&&n&i.;
%end;

run;
%mend;

 

s_lassen
Meteorite | Level 14

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.

ChrisNZ
Tourmaline | Level 20

@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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1230 views
  • 1 like
  • 3 in conversation