How to convert string to SAS text in one data step?

Reply
Occasional Contributor
Posts: 17

How to convert string to SAS text in one data step?

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;
Super User
Posts: 9,323

Re: How to convert string to SAS text in one data step?

Please format your post. Also use a proper subwindow ("little running man" or {i} icon) to post code.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 5,771

Re: How to convert string to SAS text in one data step?

So you want to create/assign macro variables based on data step variable values?
CALL SYMPUT.
Data never sleeps
Occasional Contributor
Posts: 17

Re: How to convert string to SAS text 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*/ /*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;
Super User
Posts: 9,323

Re: How to convert string to SAS text in one data step?

PLEASE FORMAT YOUR CODE!!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 17

Re: How to convert string to SAS text in one data step?

Posted in reply to KurtBremser
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)
Super User
Posts: 9,323

Re: How to convert string to SAS text in one data step?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Occasional Contributor
Posts: 17

Re: How to convert string to SAS text in one data step?

Posted in reply to KurtBremser
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. Smiley Sad
Super User
Posts: 9,323

Re: How to convert string to SAS text in one data step?


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. Smiley Sad

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 12,681

Re: How to convert string to SAS text in one data step?


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.

Super User
Posts: 6,359

Re: How to convert string to SAS text in one data step?

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.

Occasional Contributor
Posts: 17

Re: How to convert string to SAS text in one data step?

Posted in reply to Astounding
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;
Super User
Posts: 6,359

Re: How to convert string to SAS text in one data step?

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.

Occasional Contributor
Posts: 17

Re: How to convert string to SAS text in one data step?

Posted in reply to Astounding
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.
Super User
Posts: 9,323

Re: How to convert string to SAS text in one data step?

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Ask a Question
Discussion stats
  • 15 replies
  • 226 views
  • 0 likes
  • 5 in conversation