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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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