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

how to get the required output from the condition if check1-check24 = '1' then TERM=(frac1_ to frac24_). Using macro.

 

Check1_Check2_Check3_
1 1
111
111
1 1
11 
11 
1  
frac1_frac2_frac3_
Extreme  Good
GoodExtreme Good
Average Not GoodGood
Not Good Good
NeatNeat 
VbaNeat 
Average   

Required output.

 

TERM
Extreme 
Good
Average 
Not Good
Neat
Vba
Average 
Extreme 
Not Good
Neat
Neat
Good
Good
Good
Good
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Do you have IML ?

 

data have;
infile datalines dlm=',' dsd truncover;
input (frac1_ frac2_ frac3_) ($);
datalines;
Extreme,,Good
Good,Extreme,Good
Average,Not Good,Good
Not Good,,Good
Neat,Neat
Vba,Neat
Average
;

data temp;
 set have;
 array x{*} $ frac: ;
 do i=1 to dim(x);
   if not missing(x{i}) then do;name=x{i};output;end;
 end;
 keep i name;
run;
proc sort data=temp out=want;
 by i;
run;

View solution in original post

10 REPLIES 10
PeterClemmensen
Tourmaline | Level 20

I don't understand this. So if all your check variables are equal to '1', then what?

Aayushi_17
Quartz | Level 8
Then I have get the required output for term..it's s condition like if
check1="1" then term=frac1_;
andreas_lds
Jade | Level 19

@Aayushi_17 wrote:
Then I have get the required output for term..it's s condition like if
check1="1" then term=frac1_;

Sorry, i don't understand what you have and what you want. From the tables you have posted, the result seems to be a list of the non-null values of you frac-table, there seems to be no need to use the check-table at all. So ignoring "check", you should: use a data-step to transpose "frac" so that you have a table of frac-number and the value (skip missing values). then sort by frac-number -> job done.

 

Note: If you want code, you have to post data in usable form: a data step using datalines statement.

Kurt_Bremser
Super User

No macro needed. You want to stack the columns, and to do that dynamically, use sashelp.vcolumn as a source:

data have;
infile datalines dlm=',' dsd truncover;
input (frac1_ frac2_ frac3_) ($);
datalines;
Extreme,,Good
Good,Extreme,Good
Average,Not Good,Good
Not Good,,Good
Neat,Neat
Vba,Neat
Average
;

data _null_;
set sashelp.vcolumn (
  where=(libname = 'WORK' and memname = 'HAVE')
) end=eof;
if _n_ = 1 then call execute('data want (keep=term where=(term ne " ")); set');
call execute('
  have (rename=(' !! strip(name) !! '=term))
');
if eof then call execute('; run;');
run;

proc print data=want noobs;
run;

Result:

term

Extreme 
Good    
Average 
Not Good
Neat    
Vba     
Average 
Extreme 
Not Good
Neat    
Neat    
Good    
Good    
Good    
Good    
Ksharp
Super User

Do you have IML ?

 

data have;
infile datalines dlm=',' dsd truncover;
input (frac1_ frac2_ frac3_) ($);
datalines;
Extreme,,Good
Good,Extreme,Good
Average,Not Good,Good
Not Good,,Good
Neat,Neat
Vba,Neat
Average
;

data temp;
 set have;
 array x{*} $ frac: ;
 do i=1 to dim(x);
   if not missing(x{i}) then do;name=x{i};output;end;
 end;
 keep i name;
run;
proc sort data=temp out=want;
 by i;
run;
gamotte
Rhodochrosite | Level 12
Can we be assured that the initial order will be preserved for a given value of i ? Is that a documented feature ?
Ksharp
Super User

Sorry. I don't understand you .

But as from OP' data, These two tables is perfectly matched ,

therefore I assume " be assured that the initial order will be preserved".

gamotte
Rhodochrosite | Level 12

Sorry for not being clear. English is not my first language.

 

I meant that you do not have to do something like :

 

data temp;
    set have;
    array x{*} $ frac: ;

    n=_N_; /* we keep track of the initial order */

    do i=1 to dim(x);
        if not missing(x{i}) then do;name=x{i};output;end;
    end;
    keep i n name;
run;

proc sort data=temp out=want;
    by i n;
run;

 to ensure that for i=1, the order of values is the same as in column frac1_ of the initial dataset.

 

That is, the default sort algorithm for proc sort ensures that observations with the same value for the

sort criterion wil appear in the same order than before the sort.

 

Ksharp
Super User

Ha. That is what PROC SORT will consider (keep original order).

That is a big thing I love SAS.

 

Unless you used option "noequals" .

 

proc sort data=have noequals;

gamotte
Rhodochrosite | Level 12
Thanks !

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 10 replies
  • 1452 views
  • 2 likes
  • 6 in conversation