BookmarkSubscribeRSS Feed
pavank
Quartz | Level 8
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

4 REPLIES 4
PaigeMiller
Diamond | Level 26

@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)

--
Paige Miller
Astounding
PROC Star

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.

Patrick
Opal | Level 21

@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;

 

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 547 views
  • 4 likes
  • 5 in conversation