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

Hi!

 

Is it possible to change a range of variables (namepers1-namepers3)  currently a character string spearated by delimiter "/"

 

I was able to do it for one variable at a time...but for efficiency, is there a way to do Namepers1-namepsers3 in one datastep?

 

All the values in each variable are the same character string format...

 

data rr.namepers2006;
set sar_raw.rr2006 (keep=namepers1-namepers3) ;
array new_var(*)$ last rank grade;
i=1;
do while(scan(namepers1, i, "/") ne "");
new_var(i) =scan(namepers1, i, "/");
i+1;
end;
run;

 

 

 thank you!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
data want;;
infile cards dlm=',';
informat per1-per3 $20.;
input per1 per2 per3;
array name(3) $20. name1-Name3 ;
array grade(3) $20. grade1-grade3;
array rank(3) $20. rank1-rank3;   
array old_var(3) per1-per3;

do i=1 to 3;
name(i)=scan(old_var(i), 1, ".");
grade(i)=scan(old_var(i), 2, "/");
rank(i)=scan(old_var(i), 3, "/");
end;

cards;
Sample J ./0-3/ LT , Sample A ./0-1/ LT, Sample E ./0-4/ CDR
;

run;

View solution in original post

10 REPLIES 10
PaigeMiller
Diamond | Level 26

although I'm not 100% sure I understand the problem, I believe the CATX function does what you want

 

but show us an example of the input, and what the output will look like for this situation

--
Paige Miller
jenim514
Pyrite | Level 9

current data looks like this

 

namepers1 namepers2 namepers3
Sample J ./0-3/ LT Sample A ./0-1/ LT Sample E ./0-4/ CDR

 

 

Want new colums to look like this

 

last1 grade1 rank1 last2 grade2 rank2 last3 grade3 rank3
Sample J. 0-3 LT Sample A. 0-1 LT Sample E. 0-4 CDR
PaigeMiller
Diamond | Level 26

You can use the SCAN function to separate the variables into "words" where the delimiter is a slash.

 

You'd want to put this into a data step loop, probably with namepers1 through namepers8 in an array.

--
Paige Miller
jenim514
Pyrite | Level 9

I can get the san to work....just not with a range of variables.  I'm not soing something right...

 

 

data rr.namepers2006;
set sar_raw.rr2006  (keep=namepers1-namepers3);
array new_var(*)$ name1-Name3 grade1-grade3 rank1-rank3;   *not sure if you can use array dimensions like this??;
i=1;
do while(scan(namepers1-namepers3, i, "/") ne "");
new_var(i) =scan(namepers1-namepers3, i, "/");          *trying to scan range of variables;
i+1;
end;
run;

Reeza
Super User

Does the number of items vary, or is it always 3?

art297
Opal | Level 21

Here is one way:

data need (keep=id _name_ i data);
  set rr2006;
  array rec(*) $ namepers1-namepers8;
  array new_var(*) $ last rank grade;
  id=_n_;
  do i=1 to dim(rec);
    if rec(i) ne '' then do;
      j=1;
      do while(scan(rec(i), j, "/") ne "");
        data =scan(rec(i), j, "/");
        _name_=vname(new_var(j));
        output;
        j+1;
      end;
    end;
  end;
run;

proc transpose data=need out=namepers2006 (drop=id _name_);
  var data;
  by id;
  id _name_ i;
run;

Art, CEO, AnalystFinder.com

Reeza
Super User
@art297 solution is more dynamic, but if you only have 3, the code is about the same length overall. It's what you can understand enough to change 😉
Reeza
Super User
data want;;
infile cards dlm=',';
informat per1-per3 $20.;
input per1 per2 per3;
array name(3) $20. name1-Name3 ;
array grade(3) $20. grade1-grade3;
array rank(3) $20. rank1-rank3;   
array old_var(3) per1-per3;

do i=1 to 3;
name(i)=scan(old_var(i), 1, ".");
grade(i)=scan(old_var(i), 2, "/");
rank(i)=scan(old_var(i), 3, "/");
end;

cards;
Sample J ./0-3/ LT , Sample A ./0-1/ LT, Sample E ./0-4/ CDR
;

run;

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
  • 899 views
  • 2 likes
  • 4 in conversation