Help using Base SAS procedures

extract part of character string to create new varaible

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

extract part of character string to create new varaible

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.


Accepted Solutions
Solution
‎03-24-2015 02:00 PM
Super Contributor
Posts: 275

Re: extract part of character string to create new varaible

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


All Replies
Solution
‎03-24-2015 02:00 PM
Super Contributor
Posts: 275

Re: extract part of character string to create new varaible

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;

Respected Advisor
Posts: 3,124

Re: extract part of character string to create new varaible

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 199 views
  • 3 likes
  • 3 in conversation