BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
WillM
Calcite | Level 5

I use SAS EG 7.1.  I have a one record table with 150 columns, creatively named fieldname1 - fieldname150.  I would like to turn each field's value into a macro variables &fieldname1 - &fieldname150.  I have written it out as

 

data _NULL_;

     set mylib.proj_details;

     call symputx('fieldname1',fieldname1,'l');

     call symputx('fieldname2',fieldname2,'l');

     call symputx('fieldname3',fieldname3,'l');

                  ...

                  ...

     call symputx('fieldname149',fieldname149,'l');

     call symputx('fieldname150',fieldname150,'l');

run;

 

There's got to be a sexier way to do this.  I tried using a do loop but couldn't get the second argument (in which I was concatenating "fieldname" and my index variable) to be recognized.  Any ideas?  Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You can use an array to shorten your code.

 

data _NULL_;
     set mylib.proj_details;
    array field(150) fieldname1-fieldname150;

do i=1 to 150;
     call symputx(catt('fieldname', put(i, 3. -l)),field(i),'l');
end;

run;

View solution in original post

3 REPLIES 3
Reeza
Super User

You can use an array to shorten your code.

 

data _NULL_;
     set mylib.proj_details;
    array field(150) fieldname1-fieldname150;

do i=1 to 150;
     call symputx(catt('fieldname', put(i, 3. -l)),field(i),'l');
end;

run;
WillM
Calcite | Level 5

Thanks! It's beautiful.

Astounding
PROC Star

Perhaps use macro language to generate your statements:

 

%macro all150;

%local i;

%do i=1 %to 150;

call symputx("fieldname&i", fieldname&i, 'l');

%end;

run;

 

data _null_;

set mylib.proj_details;

%all150

run;

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!

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
  • 3 replies
  • 528 views
  • 2 likes
  • 3 in conversation