BookmarkSubscribeRSS Feed
Mirisage
Obsidian | Level 7

Hi Colleagues,

I was given the names of over 200 variables just as a single Excel string (attached).

I want to separate individual variable names as:

AA_VAR1

AA_VAR2

AA_VAR3

AA_VAR4

I separated them using text function of Excel like below but it took me the time almost same as doing manually.

=LEFT(B2,7)

=MID(B2,9,7)

=MID(B2,17,7)

I wonder if there is an automated way of doing using SAS?

Any help is appreciated.

Mirisage

4 REPLIES 4
PGStats
Opal | Level 21

If you ultimately want to bring these names back in Excel, the simplest thing to do is to use the Data-Conversion operation with spaces as delimiters directly in Excel. You'll get your 241 variable names in the first row which you may then paste-transpose to get a single column.

If you insist on doing this operation in SAS, then look at the SCAN function. As in :

data want;

set have;

do i=1 by 1 until(missing(name));

     name = scan(names, i);

     if not missing(name) then output;

     end;

drop names;

run;

PG

PG
Mirisage
Obsidian | Level 7

Hi PG,

Many thanks.

I have just taken a piece of that string to try your SAS code. I cannot figure out why it doesn't work. Time permitting, I wonder if you could help again.

data have;

input names $250.;

cards;

AA_VAR1 AA_VAR2 AA_VAR3 AA_VAR4 AA_VAR5 AA_VAR6 AA_VAR7 AA_VAR8 AA_VAR11 AA_VAR12;

run;

data want;

set have;

do i=1 by 1 until(missing(name));

     name = scan(names, i);

     if not missing(name) then output;

     end;

drop names;

run;

Mirisage

PGStats
Opal | Level 21

There were minor problems, corrected now:

data have;

length names $250;

input names & ;

cards;

AA_VAR1 AA_VAR2 AA_VAR3 AA_VAR4 AA_VAR5 AA_VAR6 AA_VAR7 AA_VAR8 AA_VAR11 AA_VAR12

;

data want;

length name $8;

set have;

do i=1 by 1 until(missing(name));

     name = scan(names, i);

     if not missing(name) then output;

     end;

drop names;

run;

PG

PG
Mirisage
Obsidian | Level 7

Hi PG,

A big thank for you. This is great! Worked well! Now I came to know about scan function too.

Mirisage

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 2085 views
  • 0 likes
  • 2 in conversation