BookmarkSubscribeRSS Feed
thanikondharish
Calcite | Level 5

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
Calcite | Level 5
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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 596 views
  • 1 like
  • 3 in conversation