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

Hello,

 

I have a dataset that looks like this :

 

idnamen01n02n03n04n05n06
1abcdabcd  
2qwqw    
3xx     

 

I want to separate every character of the "name" variable into the n01 to n06 variables.

 

Here is what I want it to look like:

 

idnamen01n02n03n04n05n06
1abcdabcdab
2qwqwqwqw
3xxxxxxx

 

I already separated every character of the "name" variable into multiple variable with this code (like in the first table)

data want; set have;
	length n01-n06 $1;
	array n {*} n01-n06;
	startpos = 1;
	do i = 1 to dim(n);
		length_var = vlength(n(i));
		n(i) = substr(name, startpos, length_var);
		startpos + length_var;
	end;
	drop i startpos  length_var;
run;

 

I would like to know how to fill until n06 even if the number of character in the "name" variable may vary for each observation.

 

Thank you for your help. If you need more details, please ask.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You probably want the LENGTH() function and not the VLENGTH() function since you appear to want to ignore the trailing spaces.

 

This is a good application for the MOD() function.

data want ;
 set have (keep=id name);
 array _n $1 n01-n06 ;
 do _n_=1 to dim(_n) ;
    _n(_n_)=char(name,1+mod(_n_-1,length(name)));
 end;
run;
Obs    id    name    n01    n02    n03    n04    n05    n06

 1      1    abcd     a      b      c      d      a      b
 2      2    qw       q      w      q      w      q      w
 3      3    x        x      x      x      x      x      x

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20
data have;
infile cards truncover;
input id	name	$;
cards;
1	abcd
2	qw
3	x
;

data want;
set have;
length _temp_ $100;
array t(*) $1 n01-n06;
_temp_=repeat(compress(name),dim(t)-1);
call pokelong(_temp_,addrlong(t(1)),6);
drop _:;
run;
Tom
Super User Tom
Super User

You probably want the LENGTH() function and not the VLENGTH() function since you appear to want to ignore the trailing spaces.

 

This is a good application for the MOD() function.

data want ;
 set have (keep=id name);
 array _n $1 n01-n06 ;
 do _n_=1 to dim(_n) ;
    _n(_n_)=char(name,1+mod(_n_-1,length(name)));
 end;
run;
Obs    id    name    n01    n02    n03    n04    n05    n06

 1      1    abcd     a      b      c      d      a      b
 2      2    qw       q      w      q      w      q      w
 3      3    x        x      x      x      x      x      x
novinosrin
Tourmaline | Level 20
/*linear*/
data want;
set have;
length _temp_ $100;
array t(*) $1 n01-n06;
_temp_=compress(repeat(name,dim(t)-1));
do _n_=1 to dim(t);
t(_n_)=char(_temp_,_n_);
end;
drop _:; run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 1258 views
  • 1 like
  • 3 in conversation