BookmarkSubscribeRSS Feed
caibird
Calcite | Level 5
Guys, I want to achieve below goal, but I don't know how to convert string to SAS text, could someone help me please? Thanks in advance. for string, I mean, in data step, X="AAA" then X's value AAA is string. for SAS text, I mean, %let X=AAA, then AAA here is SAS text. purpose: for vars X1-XN, step1 exclude missing value, step2 check if X1=X2=...=XN is true for false (in below case, check X1=X2=X4) must be done in one data step; data in; X1=1; X2=1; X3=.; X4=1; run; %macro diff(vars=X1 X2 X3 X4); %let vars=%sysfunc(compbl(&vars)); %let count=%sysfunc(countw(&vars,' ')); data out; set in; length _CONN $100.; _CONN=''; %do i=1 %to &count; if missing(%scan(&vars,&i))=0 then _CONN=catx('=',_CONN,"%scan(&vars,&i)"); %end; call symputx('conn'||compress(_N_),_CONN); /*my problem is here, how to convert 【_CONN】 from string to SAS text?*/ /*symget('conn'||compress(_N_)) is string, not SAS text*/ if (symget('conn'||compress(_N_))) then JUDGE=1; else JUDGE=0; run; %mend; %diff;
15 REPLIES 15
LinusH
Tourmaline | Level 20
So you want to create/assign macro variables based on data step variable values?
CALL SYMPUT.
Data never sleeps
caibird
Calcite | Level 5
data in; X1=1; X2=1; X3=.; X4=1; run; %macro diff(vars=X1 X2 X3 X4); %let vars=%sysfunc(compbl(&vars)); %let count=%sysfunc(countw(&vars,' ')); data out; set in; length _CONN $100.; _CONN=''; %do i=1 %to &count; if missing(%scan(&vars,&i))=0 then _CONN=catx('=',_CONN,"%scan(&vars,&i)"); %end; call symputx('conn'||compress(_N_),_CONN); /*my problem is here, how to convert 【_CONN】 from string to SAS text?*/ /*symget('conn'||compress(_N_)) is string, not SAS text*/ /*for string, I mean, in data step, X="AAA" then X's value AAA is string. */ /*for SAS text, I mean, %let X=AAA, then AAA is SAS text.*/ if (symget('conn'||compress(_N_))) then JUDGE=1; else JUDGE=0; run; %mend; %diff;
caibird
Calcite | Level 5
sorry, don't know how to format. I see no button or something like that in my window. let me brief: In a data set have 4 columns A B C X. Column X="A=B=C" in all rows, I want to use "A=B=C" in below statement in same data step. if (A=B=C) then ... how to convert "A=B=C" to A=B=C? (one way is call symput('_X',X), but &_X cannot use in same data step)
Kurt_Bremser
Super User

It's the 6th and 7th icons above the main posting window. Both preserve all formatting, the 7th (the "little running man") also provides enhanced-editor-like coloring.

caibird
Calcite | Level 5
sorry, I see no icon at all on post window. There is only 3 tab: rich text, html, preview. And it takes 3min to open/refresh the web. But I visit other website normal. 😞
Kurt_Bremser
Super User

@caibird wrote:
sorry, I see no icon at all on post window. There is only 3 tab: rich text, html, preview. And it takes 3min to open/refresh the web. But I visit other website normal. 😞

See here:

editor_icons.png

The active icon in this screenshot is the "little running man" icon, the next one to the left of it is the {i} icon.

ballardw
Super User

@caibird wrote:
sorry, don't know how to format. I see no button or something like that in my window. let me brief: In a data set have 4 columns A B C X. Column X="A=B=C" in all rows, I want to use "A=B=C" in below statement in same data step. if (A=B=C) then ... how to convert "A=B=C" to A=B=C? (one way is call symput('_X',X), but &_X cannot use in same data step)

Below where you see Rich Text HTML Preview, inside the window you should see a row of images starting with B. They are icons but are not buttons.

 

'Format' in this instance means to make your code legible to people by using (generally) one line per statement or instruction, indenting code between any sort of start /end block (data/run, proc/run, do/end, Select/end) so that program flow can be seen and understood easier.

Astounding
PROC Star

Mostly, the complications are from trying to get macro language to perform the functions of a DATA step.  Once you start a DATA step, following LENGTH CONN $ 100;, there should be no more macro language.  The DATA step can handle the calculations better than macro language could.  You might want a statement along these lines:

 

array mylist {*} &vars;

 

But that would be the most macro language that the DATA step requires.

caibird
Calcite | Level 5
No no, the macro must be repeatable in same data step like below, the array cannot handle that. The function of mro is: after exclude missing vars. if var1=var2=...=varn then output the row,else no output. ************************* data in; X1=1; X2=1; X3=.; X4=1; Y1=2; Y2=4; Y3=17; Z1='A'; Z2='BC'; Z3='C'; Z4=''; ...; run; data out; set in; %mro(vars=X1 X2 X3 X4),out=); %mro(vars=Y1 Y2 Y3),out=); %mro(vars=Z1 Z2 Z3 Z4),out=); ...; run;
Astounding
PROC Star

If you sketch out what the program should do, with no macro language involved, you will find ways to make a DATA step do what you ask.  For example, here are some possibilities:

 

if min(mylist{*}) = max(mylist{*}) then output;

 

Or possibly:

 

do i=1 to dim(mylist);

   if mylist{i} > ' ' then conn = catx(' ', conn, vname(mylist{i}));

end;

 

It starts with knowing the goal ... what is the DATA step that would work, if no macro language were involved.

caibird
Calcite | Level 5
thanks for reply. but actually my question is how to convert string to SAS text in same data step. I mean somehow I need to get a string like [A=B=C] from a data set's var, but then how to convert it to SAS text A=B=C so allow me to use if (A=B=C) then in the same data step.
Kurt_Bremser
Super User

The data step is compiled before execution. You cannot change the code while it executes.

You can only execute already-written code conditionally with if-then-else or select()-when()-end.

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
  • 15 replies
  • 2215 views
  • 0 likes
  • 5 in conversation