- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Want to use variables first and last names from my data set to compute name in the form first, one space, last.
Want to call firstlast assigning length. Then using strip function.
Then i want to use name in the form last, a comma, one space, first. Calling it lastfirst and assigning appropriate length. and using strip function .
Data is :
obs Name first last
1 Rahul David Anand Rahul Anand
2 David Tel Merck Boxer David Boxer
3 Jim carney Ramsey Jim Ramsey
4 George Uday Yale George Yale
5 Frank Bates Frank Bates
6 Stacy Mary kim Coghlan Stacy Coghlam
my code which i am trying is not giving me accurate results :I would be grateful if anybody can assist.
length firstlast $50 ;
firstlast=catx(' ',first,last);
first=strip(first);
last=strip(last);
run;
length lastfirst $50;
lastfirst=catx(' ',last,',',first);
first=strip(first);
last=strip(last);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Look at SCAN and COUNTW functions.
Last_index = countw(name);
first = SCAN(name, 1);
last = scan(name, last_index);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You're not using CATX properly. Review the documentation and the examples in the documentation. Post back if you can't get it working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@afs wrote:
I asked a simple question. I know that i can read the documentation. Why you are replying like i am kid and don't know where to read and what to read. I just wanted help to get required results. I know every thing about scan and countw.And what you have suggested will never work.Let somebody else help me and you stay easy.Thanks
I interpreted your question incorrectly, but your response is childish.
Try using ', ' as the first argument to CATX for your second step.
Good luck and good bye.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
What are your results and why do you think it is not accurate ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
When i run the first code its seems ok , but when i run the second part - the log gives error in length ,
I have tried scan , but it doesnt work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You have not post your results and what error you have.
I can't run your code now.
what is the error message ? usually SAs messages are clear enough to overcome.
You can use an alternative code:
firstlast = strip(first} || ' ' || strip(last);
lastfirst = strip(last) || ',' || strip(first);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
length firstlast $50 lastfirst $50;
firstlast = strip(first} || ' ' || strip(last);
lastfirst = strip(last) || ',' || strip(first);
run;
log error:
29 firstlast = strip(fitst} || ' ' || strip(last);
_______
180
ERROR 180-322: Statement is not valid or it is used out of proper order.
30
31 lastfirst = strip(last) || ',' || strip(first);
_________
180
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The message: Statement is not valid or it is used out of proper order.
menas that code is out of proper order. It should be like:
data want;
set have;
... enter any code ...
run;
It seems that you submitted lines without the first two lines ?!
I got the error - you used different brackets : ( } instead ( )
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
your code should work too. @Reeza was right:
length lastfirst $50;
/* lastfirst=catx(' ',last,',',first); <<< replace with next line */
lastfirst(',',last,first);
first=strip(first);
last=strip(last);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Look at SCAN and COUNTW functions.
Last_index = countw(name);
first = SCAN(name, 1);
last = scan(name, last_index);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Dear Shmuel,
Just wanted to know if like in the data set there is a person who has 4 names and need to get his first middle name like David Tel Merck Boxer so Tel is his middle name , and there are people who have only two names so no middle is there or we can say there would be blank. So i want to create a variable middle with length $25 .So is that also possible , ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
You can use function COUNTW (meaning: Count words) and then check and decide
what to assign to the new variable:
if countw(names) > 2 then mid_name = scan(names,2);