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: 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 25. 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
  • 960 views
  • 5 likes
  • 3 in conversation