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.
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;
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;
This may not be the answer you are waiting for, as it may only have uncompleted code or concept
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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.