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

Hi All, 

 

Thanks for any help in advance.

 

I have a dataset that is like this:

 

Key   Width

123   99,99

456   99

789   87,99,142

 

What I wish I had was the following:

 

Key   NewWidth    Number

123   99                1

123   99                2

456   99                1

789   87                1

789   99                2

789   142              3

 

I have searched a few of the messages and gotten some things to work but they require the NewWidth to always be the same # of characters...which it can be 2 or 3 characters.  So far I have attempted using substr(), scan(), countc()...nothing has clicked.

 

Again, thanks for any pointers!!!

 

1 ACCEPTED SOLUTION

Accepted Solutions
HeidiChi
Calcite | Level 5

Well - turns out I should have just kept trying!!  Just solved it...

 

data test (keep=Key NewWidth Number);

set a.datasetname;
count=count(width,',');
put count=;

do i=1 to (count+1);
NewWidth=scan(width,i,',');
Number=i;
output;
end;
run;

View solution in original post

3 REPLIES 3
HeidiChi
Calcite | Level 5

Well - turns out I should have just kept trying!!  Just solved it...

 

data test (keep=Key NewWidth Number);

set a.datasetname;
count=count(width,',');
put count=;

do i=1 to (count+1);
NewWidth=scan(width,i,',');
Number=i;
output;
end;
run;

slchen
Lapis Lazuli | Level 10
data want;
   set have;
   number=0;
   do i=1 by 1;
      newwidth=scan(width,i);
	  if not missing(newwidth) then do;
      number+1;
	  output;
	  end;
	  if missing(newwidth) then leave;
   end;
   drop i width;
run;
	  
PGStats
Opal | Level 21

It is not clear whether newWidth should be character or numeric. Here is a way to get both:

 

data want;
set have;
do number = 1 by 1;
    newWidth = scan(width, number, ",");
    if missing(newWidth) then leave;
    numWidth = input(newWidth, best.);
    output;
    end;
keep key newWidth number numWidth;
run;
PG

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2287 views
  • 4 likes
  • 3 in conversation