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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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