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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

10 REPLIES 10
ballardw
Super User

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}

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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);

 

ballardw
Super User

@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.

Astounding
PROC Star

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

data want;
  set have;
  d=char(name,1);
e=char(gen,1);
run;
mehul4frnds
Obsidian | Level 7

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

mehul4frnds
Obsidian | Level 7

Thanks for such an apt solution

mehul4frnds
Obsidian | Level 7

Thanks, this also worked perfectly!!!

Cynthia_sas
SAS Super FREQ

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

mehul4frnds
Obsidian | Level 7

Thanks, Cynthia for such an insightful reply. 

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
  • 10 replies
  • 1980 views
  • 4 likes
  • 5 in conversation