DATA Step, Macro, Functions and more

function

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

function

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


Accepted Solutions
Solution
a week ago
Occasional Contributor
Posts: 15

Re: function

Posted in reply to GAUTAMDVN
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


All Replies
Super User
Posts: 9,890

Re: function

Posted in reply to GAUTAMDVN

Post the code and log you already have, and show where it does not meet your expectations.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 19

Re: function

Posted in reply to KurtBremser

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 ;

Occasional Contributor
Posts: 19

Re: function

Posted in reply to GAUTAMDVN

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 ;

Super User
Posts: 9,890

Re: function

Posted in reply to GAUTAMDVN

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 17

Re: function

Posted in reply to GAUTAMDVN

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;

Solution
a week ago
Occasional Contributor
Posts: 15

Re: function

Posted in reply to GAUTAMDVN
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;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 192 views
  • 2 likes
  • 4 in conversation