BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GAUTAMDVN
Calcite | Level 5

Create a new variable last name from name, find the starting position of first and create new variable first name from name variable using the positions found in previous question.Hint: Use SUBSTR function

1 ACCEPTED SOLUTION

Accepted Solutions
mahesh146
Obsidian | Level 7
Please check the below:
PROC IMPORT DATAFILE='/folders/myfolders/sasuser.v94/flat files/test/department.csv'
DBMS=CSV
OUT=TEMP
REPLACE
;
RUN;

DATA TEMP;
SET TEMP;

Last_Name=SCAN(Name,1,',');
Tmp_fst_nm=SCAN(Name,2,',');

Fst_Nm= SUBSTR(Tmp_fst_nm,1,LENGTH(Tmp_fst_nm)-2);

DROP Tmp_fst_nm;
RUN;

View solution in original post

6 REPLIES 6
GAUTAMDVN
Calcite | Level 5

DATA PART_A ;
SET LEARN.DEPARTMENT(KEEP = NAME) ;
LAST_NAME = SCAN(NAME,1);
RUN ;

 

 

DATA PART_B;
SET LEARN.DEPARTMENT (KEEP = NAME);
NAME = COMPRESS(NAME,,'P') ;
FIRST_NAME = SCAN(NAME,2,'');
CHAR = SUBSTR(FIRST_NAME,1,1) ;
IF INDEX(CHAR,'') > 0 ;
RUN ;

GAUTAMDVN
Calcite | Level 5

Hi Kurt,

 

Part a is done by me but in second part I've tried to find the starting position of first name as mentioned below but not able to do the  same.

 

 

DATA PART_B;
SET LEARN.DEPARTMENT (KEEP = NAME);
NAME = COMPRESS(NAME,,'P') ;
FIRST_NAME = SCAN(NAME,2,'');
CHAR = SUBSTR(FIRST_NAME,1,1) ;
IF INDEX(CHAR,'') > 0 ;
RUN ;

Kurt_Bremser
Super User

Here's code that does what I think you want, retrieving first_name in two ways:

data department;
infile '$HOME/sascommunity/department.csv' dlm=',' dsd termstr=CRLF firstobs=2;
input code_id $ department :$30. expenditure :comma. name :$30.;
run;

data want;
set department (keep=name);
last_name = scan(name,1,',');
first_name = left(scan(name,2,','));
first_name = substr(first_name,1,length(first_name)-2);
start_pos = indexc(name,',') + 2;
first_name_2 = substr(name,start_pos);
run;

The additional substr in

first_name = substr(first_name,1,length(first_name)-2);

removes the country code.

ruchi11dec
Obsidian | Level 7

I am not sure, if I understood the requirement correctly, but as far as i understood this is the solution :

 

data new;
set department;
last_name= scan(names,2,',');
pos=indexc(names,',');
first_name = substr(names,1,pos-1);
drop pos;
run;

mahesh146
Obsidian | Level 7
Please check the below:
PROC IMPORT DATAFILE='/folders/myfolders/sasuser.v94/flat files/test/department.csv'
DBMS=CSV
OUT=TEMP
REPLACE
;
RUN;

DATA TEMP;
SET TEMP;

Last_Name=SCAN(Name,1,',');
Tmp_fst_nm=SCAN(Name,2,',');

Fst_Nm= SUBSTR(Tmp_fst_nm,1,LENGTH(Tmp_fst_nm)-2);

DROP Tmp_fst_nm;
RUN;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 2199 views
  • 2 likes
  • 4 in conversation