DATA Step, Macro, Functions and more

how to extract initial character from the character variable using array ?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

how to extract initial character from the character variable using array ?

Hi,

 

I want to extract initial one character from the character variables using an array and create two new variable (D, E). 

 

My data look like this

data have1;
input id name$ gen$ PROCHI DATE A B C;
datalines;
1 m_ram male 1 1.1 1 . 1.23456
2 m_mohan male 1 1.1 99 1 1.23456
3 m_shyam male 2 2.1 1 99 1.23456
4 f_radha female 2 2.1 90 1 99
5 f_gita female 2 2.1 99 95 1.23456
6 f_sita female 2 3.1 . 1 1.23456
7 f_ranu female 3 2.1 1 . 1.23456
8 f_tina female 3 2.1 . 1 1.23456
;
run;

 

I want data like this

 

id  name$     gen$     ID    DATE A    B    C            D   E;
1 m_ram       male      1    1.1      1     .    1.23456   m m
2 m_mohan  male      1    1.1     99    1   1.23456   m m
3 m_shyam  male       2    2.1      1  99   1.23456   m m
4 f_radha      female    2   2.1    90    1  99              f   f
5 f_gita         female    2    2.1   99   95    1.23456  f   f
6 f_sita          female   2    3.1     .      1    1.23456  f   f
7 f_ranu        female   3     2.1    1      .     1.23456  f   f
8 f_tina         female    3    2.1     .      1     1.23456 f   f

I have tried following code:

 

data want (drop=i) ;
set have1 ;
array try {2} $ name gen ;

array try2 {i} $ D E;
do i = 1 to 2;
try2 {i} = substr {try2{i},1,1} ;
end;
run;

 

But it's not working. It says 'undeclared array referenced' substr 

 

Kindly suggest

 

Thanks in advance

 

 


Accepted Solutions
Solution
‎04-06-2018 05:30 AM
Super User
Super User
Posts: 9,195

Re: how to extract initial character from the character variable using array ?

Substr is a function, therefore the use of curly braces isn't going to work is it:

try2 {i} = substr {try{i},1,1};

Should it not be:

try2 {i} = substr(try{i},1,1);

 

View solution in original post


All Replies
Super User
Posts: 13,008

Re: how to extract initial character from the character variable using array ?

Posted in reply to mehul4frnds

You referenced the same array, the unpopulated one on both side of the =

try2 {i} = substr {try2{i},1,1}

use

 

try2 {i} = substr {try{i},1,1}

 

 

Solution
‎04-06-2018 05:30 AM
Super User
Super User
Posts: 9,195

Re: how to extract initial character from the character variable using array ?

Substr is a function, therefore the use of curly braces isn't going to work is it:

try2 {i} = substr {try{i},1,1};

Should it not be:

try2 {i} = substr(try{i},1,1);

 

Super User
Posts: 13,008

Re: how to extract initial character from the character variable using array ?


@RW9 wrote:

Substr is a function, therefore the use of curly braces isn't going to work is it:

try2 {i} = substr {try{i},1,1};

Should it not be:

try2 {i} = substr(try{i},1,1);

 


Good catch, my monitor resolution makes it hard to see curly braces so I don't use them.

Super User
Posts: 6,534

Re: how to extract initial character from the character variable using array ?

Posted in reply to mehul4frnds

SUBSTR does not use curly brackets.  Use parentheses instead.  Note that the second array name should be TRY, not TRY2.

 

try2 {i} = substr (try{i},1,1) ;

 

Alternatively, get clever and get rid of SUBSTR:

 

array try2 {i} $1 D E;
do i = 1 to 2;
try2 {i} = try{i} ;
end;

 

Because the array defines the new variables as 1 character long, you only have room to store the first character.

Super User
Super User
Posts: 9,195

Re: how to extract initial character from the character variable using array ?

Posted in reply to Astounding

The array and loop is a bit pointless unless learning arrays:

data want;
  set have;
  d=char(name,1);
e=char(gen,1);
run;
Occasional Contributor
Posts: 16

Re: how to extract initial character from the character variable using array ?

Thanks for the reply. This is really a good way to get rid of ARRAY

Occasional Contributor
Posts: 16

Re: how to extract initial character from the character variable using array ?

Posted in reply to Astounding

Thanks for such an apt solution

Occasional Contributor
Posts: 16

Re: how to extract initial character from the character variable using array ?

Posted in reply to Astounding

Thanks, this also worked perfectly!!!

SAS Super FREQ
Posts: 9,254

Re: how to extract initial character from the character variable using array ?

Posted in reply to mehul4frnds

Hi:

  You could do all your manipulation in the original program. I don't actually see that using an ARRAY is necessary for what your stated purpose is. I also don't see the need to read ID and PROCHI with those names if you want PROCHI to be called ID in the final output. You might also use the SCAN function to get rid of the m_ or f_ in front of name (assuming that if you substring it out you want name without that string).

dont_need_array.png

 

Hope this gives you some suggestions for a different approach.

 

Cynthia

Occasional Contributor
Posts: 16

Re: how to extract initial character from the character variable using array ?

Posted in reply to Cynthia_sas

Thanks, Cynthia for such an insightful reply. 

☑ This topic is solved.

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

Discussion stats
  • 10 replies
  • 115 views
  • 4 likes
  • 5 in conversation