data have;
input Name $ Gender $ City $12.;
datalines;
Srilakshmi Female Hyderabad
Vamika Female Bangalore
Prajwal Male Mumbai
;
run;
data want;
set have;
City = substr(City, 1, 1);
Gender = substr(Gender, 1, 1);
Name = substr(scan(Name, 1), 1, 1);
drop Name Gender City;
left_to_right=catx(' ',name,gender,city);
right_to_left=catx(' ',city,gender,name);
run;
Hi Guys ,
Here i want to extract first letter from each variable left to right and right to left
is it correct way or any other method to achieve could you please give solution
@pavank wrote:
Here i want to extract first letter from each variable left to right and right to left
is it correct way or any other method to achieve could you please give solution
You don't have to ask us "is it correct". You run the code and see if it is correct.
This is a relatively short piece of code and if it does what you want, then I wouldn't bother searching for another solution (unless your question is purely to learn alternatives)
It looks like your program drops the original variabes. Nothing wrong with that, but if you don't need them this simplification comes to mind:
data want;
length city gender name $ 1;
set have;
drop Name Gender City;
left_to_right=catx(' ',name,gender,city);
right_to_left=catx(' ',city,gender,name);
run;
Just shorten the variables, and forget about SUBSTR.
Check or the FIRST function.
@pavank Nothing wrong with your code but there are of course almost always a multitude of coding options. Here one more:
data want;
set have;
length left_to_right right_to_left $5;
keep left_to_right right_to_left;
left_to_right=catx(' ',first(name),first(gender),first(city));
right_to_left=reverse(left_to_right);;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.