I want to extract the first name from a name with a space and also reverse it, and here is my codes:
PROC IMPORT DATAFILE='D:\Wow.xlsx' OUT= Wow DBMS= XLSX REPLACE; RUN; DATA Names; SET Wow; First_Name = SCAN(REVERSE(Reversed_names),1); RUN;
The result is:
The content is empty, which is not what I want. Could anyone tell me what is the problem with my codes? (see attachment with the dataset)
I think @stat_sas is close.
Check your variable properties. Since you're using EG, it looks like it's allowing spaces in the variable name.
So you need to refer to your variable as
'Reversed Names'n
or set the following option so it converts spaces to underscores when you import your data.
Option validvarname = v7;
Otherwise your code is correct.
Hi,
This works fine for me.
data Wow;
input Name $20.;
datalines;
lazraM sérdnA
ladiV euqirnE
;
DATA Names;
SET Wow;
First_Name = SCAN(REVERSE(Name),1);
RUN;
Problem seems to be in variable reference
SCAN(REVERSE(Reversed_names),1);
Variable Reversed_names is not contained in your data set wow
Your code is fine. It is proabably your data that is messed up. Most likely you have some non-printing charater at the end of the line that is being interpretted by SCAN() as the first word.
Your XLSX file is fine, but perhaps you actually read the data from a text file and did not properly tell SAS what characters to use for end of line and ended up with a Carriage Return character at the end of each value?
libname x xlsx "&path/Wow.xlsx";
data want;
set x.sheet1 ;
first_name= scan(reverse(reversed_names),1);
put (_all_) (=);
first_name= scan(reverse(reversed_names||'0D'x),1);
put (_all_) (=);
run;
Results
Reversed_Names=lazraM sérdnA first_name=Andrés
Reversed_Names=lazraM sérdnA first_name=
Reversed_Names=ladiV euqirnE first_name=Enrique
Reversed_Names=ladiV euqirnE first_name=
I think @stat_sas is close.
Check your variable properties. Since you're using EG, it looks like it's allowing spaces in the variable name.
So you need to refer to your variable as
'Reversed Names'n
or set the following option so it converts spaces to underscores when you import your data.
Option validvarname = v7;
Otherwise your code is correct.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.