BookmarkSubscribeRSS Feed
cm3
Fluorite | Level 6 cm3
Fluorite | Level 6

Hi

Pls help with this logic to write in SAS code

If response contains 'i1'/'i2'/'i3', Res=A or B or C respectively.

for ex:   response=i1,i2 then Res=AB

response=i1,i3 then Res=AC

 

Thnks

 

5 REPLIES 5
Kurt_Bremser
Super User
ballardw
Super User

Without seeing more examples of actual data I would start with something like this:

data want;
   set have;
   length res $ 3;
   if findw(response,'i1',"/'")>0 then res='A';
   if findw(response,'i2',"/'")>0 then res=cats(res,'B'); 
   if findw(response,'i3',"/'")>0 then res=cats(res,'C'); 
run;
cm3
Fluorite | Level 6 cm3
Fluorite | Level 6

sorry, did not work

ballardw
Super User

@cm3 wrote:

sorry, did not work


"Did not work" is awful vague.

Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.

Unexpected output? Provide input data in the form of data step code pasted into a code box, the actual results and the expected results. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Since you did not provide any actual data other than a string that by face value is invalid in SAS, to have imbedded ' as you you use the definition would have to have " " around the entire value, I provided a guess.

art297
Opal | Level 21

@ballardw's suggestion didn't include looking for commas, and was case sensitive. Does the following work on your data?:

data have;
  input response $;
  cards;
I1,I3
I3,I2
i1,i2,i3
I1,I2
I2,I3
;

data want;
  set have;
  length res $3;
  if findw (Response,'I1',"/,",'i') then Res='A';
  if findw (Response,'I2',"/,",'i') then Res=catt(Res,'B');
  if findw (Response,'I3',"/,",'i') then Res=catt(Res,'C');
run;

Art, CEO, AnalystFinder.com

 

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