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;
Look at SCAN and COUNTW functions.
Last_index = countw(name);
first = SCAN(name, 1);
last = scan(name, last_index);
You're not using CATX properly. Review the documentation and the examples in the documentation. Post back if you can't get it working.
@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.
What are your results and why do you think it is not accurate ?
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);
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 ( )
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);
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 , ?
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);
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Select SAS Training centers are offering in-person courses. View upcoming courses for: