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