BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

Hi, I'm trying to use the variables in this data as values for a new variable i'm trying to create.

 

There are 4 subjects, and the "x" represents the value that i want presented in a new variable called CRIT.

So if zzinc3="x", then CRIT=I3.

If zzinc3="x", zzinc10="x", and zzexc7="x" then CRIT=I3, I10, E7

data have; 
infile datalines dsd dlm=",";
	input subject $ zzinc1 $ zzinc2 $ zzinc3 $ zzinc4 $ zzinc5 $ zzinc6 $ zzinc7 $ zzinc8 $ zzinc9 $ zzinc10 $ zzinc11 $ zzexc1 $ zzexc2 $ zzexc3 $ zzexc4 $ zzexc5 $ zzexc6 $ zzex7 $;
datalines;
001, , , x, , , , , , , x, x, , , , , x, x, 
002, x, , , , x, x, , , , , , , x, , , x, ,
003, , x, x, , , , , , , , , , , , x, , , 
004, , , , , , , , , , , , , , , , , ,  
;
run;

 

Desired output:

 

subject      CRIT

001           I3, I10, I11, E5, E6

002           I1, I5, I6,  E2, E5

003           I2, I3, E4

004

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

I ask my usual question ... why are you doing this, what can you do with data in CRIT that you can't do with data in the original form? Often, people think that converting the data to a new form makes things easier, but I am always skeptical. It might be easier, or it might not be, depending on the next step, and you didn't tell us what happens next.

 

Anyway, here is code to convert the data

 

data want;
    set have;
    length crit $ 200;
    array zzi zzinc:;
    array zze zzexc:;
    crit=' ';
    do i=1 to dim(zzi);
        if zzi(i)='x' then crit=cats(crit,',I',i);
    end;
    do i=1 to dim(zze);
        if zze(i)='x' then crit=cats(crit,',E',i);
    end;
    if crit=:',' then crit=substr(crit,2);
    drop i;
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26

I ask my usual question ... why are you doing this, what can you do with data in CRIT that you can't do with data in the original form? Often, people think that converting the data to a new form makes things easier, but I am always skeptical. It might be easier, or it might not be, depending on the next step, and you didn't tell us what happens next.

 

Anyway, here is code to convert the data

 

data want;
    set have;
    length crit $ 200;
    array zzi zzinc:;
    array zze zzexc:;
    crit=' ';
    do i=1 to dim(zzi);
        if zzi(i)='x' then crit=cats(crit,',I',i);
    end;
    do i=1 to dim(zze);
        if zze(i)='x' then crit=cats(crit,',E',i);
    end;
    if crit=:',' then crit=substr(crit,2);
    drop i;
run;
--
Paige Miller
Hello_there
Lapis Lazuli | Level 10
Hi PaigeMiller,
This is how the raw data is presented to me, and I'm creating an output that summarizes a list of those variables marked "x", because they represent criterias that were not met for each subject. It is better to present it is a comma-separated list condensed into one variable, rather than all 18 variables be presented. The desired output is the final step.

Thanks for your help!

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 133 views
  • 1 like
  • 2 in conversation