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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 410 views
  • 1 like
  • 2 in conversation