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 posted a question earlier that was answered by @PaigeMiller , and I got a solution.

However, this new data set has a twist. Some variables have letters attached to them.

 

The purpose of this is that I was given raw data, and I'm creating an output that condenses information into one variable.

 

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 zzinc2a="x" and zzexc2b="x"  and zzexc3="x" then CRIT=I2A, E2B , E3 ** new twist**

New data with a twist:

data have2; 
infile datalines dsd dlm=",";
	input subject $ zzinc1 $ zzinc2a $ zzinc2b $ zzinc2c $ zzinc3 $ zzexc1 $ zzexc2a $ zzexc2b $ zzexc3 $;
datalines;
001, x, x, , x, , , x, , 
002, , x, , , x, , , ,
003, , x, , x, , , , ,
004, , , , , x, , , ,x
;
run;

desired output:

subject    CRIT

001         I1, I2A, I2C, E2A

002         I2A, I3

003         I2A, I2C,

004         I3, E3

 

@PaigeMiller 's solution below for part1

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;
    keep subject crit;
run;

 

link to part1: https://communities.sas.com/t5/SAS-Programming/How-do-I-turn-variables-into-values-for-a-new-variabl...

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data want2;
    set have2;
    length crit $ 200;
    array zzi zzinc:;
    array zze zzexc:;
    crit=' ';
    do i=1 to dim(zzi);
        if zzi(i)='x' then do;
            varname=vname(zzi(i)); 
            crit=cats(crit,', I',substr(varname,6));
        end;
    end;
    do i=1 to dim(zze);
        if zze(i)='x' then do;
            varname=vname(zze(i)); 
            crit=cats(crit,', E',substr(varname,6));
        end;
    end;
    if crit=:',' then crit=substr(crit,2);
    drop i varname;
    keep subject crit;
run;
--
Paige Miller

View solution in original post

2 REPLIES 2
PaigeMiller
Diamond | Level 26
data want2;
    set have2;
    length crit $ 200;
    array zzi zzinc:;
    array zze zzexc:;
    crit=' ';
    do i=1 to dim(zzi);
        if zzi(i)='x' then do;
            varname=vname(zzi(i)); 
            crit=cats(crit,', I',substr(varname,6));
        end;
    end;
    do i=1 to dim(zze);
        if zze(i)='x' then do;
            varname=vname(zze(i)); 
            crit=cats(crit,', E',substr(varname,6));
        end;
    end;
    if crit=:',' then crit=substr(crit,2);
    drop i varname;
    keep subject crit;
run;
--
Paige Miller
Hello_there
Lapis Lazuli | Level 10
Thanks, PaigeMiller. Very helpful!

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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