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

Hi All

I need help to find the best way to extract part of my character string which is like this:

choice(Var name)

yes

yes

no

yes-book

yes-internet

no

.

.

.

.

., now I need to create a new variable from this variable by deciding if it has a yes or yes-"something" then new var = 1 else if its no, new var=0;

I can do it but looking for the best way to do so.

Second problem,

I have to rename all variables in my data set, which are like 1 , 2 , 3 ,4 and so in original dataset. I read the excel file in sas and it transformed them into _1, _2 , _3 and so on . I want to change them all  to say Q1, Q2, and so on. Please suggest an efficient way.

Thanks for you time.

1 ACCEPTED SOLUTION

Accepted Solutions
slchen
Lapis Lazuli | Level 10

Flag variable:

data have;

input string $20.;

_string=findw(string,'yes','_','ik')>0;

cards;

yes

yes

no

yes-book

yes-internet

no

no_book

New YES

;

run;

Change variables names:

data want;

   set have;

   array chars _character_;

   array nums _numeric_;

  do over chars;

      chars=translate(chars,'Q', '_');

  end;

do over nums;

      nums=translate(nums,'Q', '_');

  end;

run;

View solution in original post

2 REPLIES 2
slchen
Lapis Lazuli | Level 10

Flag variable:

data have;

input string $20.;

_string=findw(string,'yes','_','ik')>0;

cards;

yes

yes

no

yes-book

yes-internet

no

no_book

New YES

;

run;

Change variables names:

data want;

   set have;

   array chars _character_;

   array nums _numeric_;

  do over chars;

      chars=translate(chars,'Q', '_');

  end;

do over nums;

      nums=translate(nums,'Q', '_');

  end;

run;

Haikuo
Onyx | Level 15

This may not be the answer you are waiting for, as it may only have uncompleted code or conceptSmiley Happy

1. Any of these functions will work for you: Index, Indexw, find, findw, prxmatch.

2. If your table is huge, I 'd recommended you to use Proc datasets + Macro to rename, Macro is to be generated using dictionary tables. If your table is not that big, then the following should work:

data want;

set  have;

rename _1-_10=Q1-Q10;

run;

Good luck,

Haikuo

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!

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
  • 2 replies
  • 985 views
  • 3 likes
  • 3 in conversation