BookmarkSubscribeRSS Feed
raja777pharma
Fluorite | Level 6

Hello ,

 

I am working on macro to create variables names with suffix sequence  number  where value by condition.

 

In one variable data is separated by delimiter ' ; ' ,  bases on this need to separate strings and find where string has value '=1' and take those strings by sequence and create variable names by sequence.

In Below data last string does not have delimiter ':' , i am confused how to take that one.

 

data test;

x="RIB 1 RIGHT=1;RIB 1 LEFT=0;RIB 2 RIGHT=1;RIB 2 LEFT=0;RIB 3 RIGHT=0;RIB 3 LEFT=1;
RIB 4 RIGHT=0;RIB 4 LEFT=1;RIB 5 RIGHT=0;RIB 5 LEFT=1;RIB 6 RIGHT=0;RIB 6 LEFT=0;
RIB 7 RIGHT=0;RIB 7 LEFT=1;RIB 8 RIGHT=0;RIB 8 LEFT=0;RIB 9 RIGHT=0;RIB 9 LEFT=0;
RIB 10 RIGHT=0;RIB 10 LEFT=1;RIB 11 RIGHT=1;RIB 11 LEFT=1;RIB 12 RIGHT=0;RIB 12 LEFT=0;
STERNUM=0;CLAVICLE RIGHT=0;CLAVICLE LEFT=0;SCAPULA RIGHT=0;SCAPULA LEFT=1";

run;

%macro LOC(var=);
%do k=1 %to 40;    
    if index(scan(&var,&k,';'),'=1')>0 then x_&k=scan(&var,&k,';');
    else if scan(&var,&k,'=1')>0 then x_&k=scan(&var,&k,';');
 
    /*Separating above concatenated into different variables */
    if scan(vari,&k,'=1') ne '' then loc&k=scan(vari,&k,'=1'); 
    else LOC1=scan(vari,1,'=1');
%end;
%mend;
 
data sk;
 set test;
 %loc(var=x);
run;

Need output data set as below :

 

raja777pharma_0-1599650254536.png

 

 

 

 

 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

There's no need for a macro, this can all be done in a DATA step.

 

data want;
    set test;
	array v $16 var1-var100;
	count=0;
	do i=1 to countw(x,';');
		text=scan(x,i,';');
		if find(text,'=1') then do;
			count=count+1;
			v(count)=text;
		end;
	end;
	drop i count;
run;

I assumed a maximum of 100 variables in this example, but that's easily changed if you have more or fewer variables.

--
Paige Miller
raja777pharma
Fluorite | Level 6
Hi Paige Miller,
Thank you for solution.
is any way to restrict variables created based on value '=1'.
Don't want create any extra variables
PaigeMiller
Diamond | Level 26

@raja777pharma wrote:

is any way to restrict variables created based on value '=1'.

I don't understand. My answer has values that only contain =1

 

Don't want create any extra variables

Yes, of course you can eliminate the unneeded variables after the fact. (PROC TRANSPOSE, eliminate rows where the value is missing, then another PROC TRANSPOSE if you really have to have the wide formatted data set)

 

Which brings me to the question, why do you need data in this unusual form? A long form rather than a wide data set would make this activity of not having unneeded variables much easier. I'm thinking that there may be much easier ways to move forward and to handle this problem, but at this time, I am not aware of the entire big picture.

--
Paige Miller

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 913 views
  • 0 likes
  • 2 in conversation