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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 2004 views
  • 5 likes
  • 3 in conversation