BookmarkSubscribeRSS Feed
pavank
Obsidian | Level 7
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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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