BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
aalluru
Obsidian | Level 7

I want to retain column names in the alphabetical order. I already have another table which has a single column consisting of these names in alphabetical order. Can I somehow incorporate this into the retain statement instead of typing all the column names manually?

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

@aalluru wrote:

I'm getting an error for each column saying:

ERROR: Alphabetic prefixes for enumerated variables (<column name>) are different.


This is how SAS punishes people who insist on using non-standard variable names. ;-)

 

The NLITERAL function should resolve the issue, though:

select nliteral(name) into :names separated by " "

View solution in original post

5 REPLIES 5
Kurt_Bremser
Super User
proc sql noprint;
select name into :names separated by " "
from dictionary.columns
where libname = "LIBNAME" and memname = "DSNAME"
order by name;
quit;

data want;
retain &names.;
set libname.dsname;
run;
FreelanceReinh
Jade | Level 19

@aalluru wrote:

I already have another table which has a single column consisting of these names in alphabetical order. Can I somehow incorporate this into the retain statement instead of typing all the column names manually?


Yes, in Kurt_Bremser's code replace

dictionary.columns
where libname = "LIBNAME" and memname = "DSNAME"

with the name of that other table and replace "name" with the name of its single column.

 

Alternatively, you could use CALL SYMPUTX in a data step to write the variable names from that table (concatenated with the CATX function) into macro variable names.

aalluru
Obsidian | Level 7

I'm getting an error for each column saying:

ERROR: Alphabetic prefixes for enumerated variables (<column name>) are different.

FreelanceReinh
Jade | Level 19

@aalluru wrote:

I'm getting an error for each column saying:

ERROR: Alphabetic prefixes for enumerated variables (<column name>) are different.


This is how SAS punishes people who insist on using non-standard variable names. ;-)

 

The NLITERAL function should resolve the issue, though:

select nliteral(name) into :names separated by " "
aalluru
Obsidian | Level 7
haha that worked, thank you so much!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1029 views
  • 5 likes
  • 3 in conversation