BookmarkSubscribeRSS Feed
KimC
Calcite | Level 5
Hi all,

I need a bit of help getting SAS to accept dynamically generated data in an input statement. Consider this data step (fictionary data):

data DEAdata;
format firms $12.;
input firms $ object con1-con9;
datalines;
beta 1 2 &x 0 4 1 2 2 1 2
La0 1 2 3 0 4 1 2 2 1 2
Lb 1 2 3 0 4 1 2 2 1 2
Lc 1 2 3 0 4 1 2 2 1 2
Ld 1 2 3 0 4 1 2 2 1 2
Le 1 2 3 0 4 1 2 2 1 2
Lf 1 2 3 0 4 1 2 2 1 2
_RHS_ 1 2 3 0 4 1 2 2 1 2
;

My problem is the "x" variable in the first row which is a global variable(hence the &) that is assigned a value dynamically prior to this data step when the script is run. But SAS does not accept that I place the variable directly in the datalines as above. It is deemed invalid data in the log. Can anyone help me on this one? I can only come up with one possible explanation. Earlier in the script I use the "call symput" routine in order to make "x" a global variable. As far as I can tell this routine converts any numeric data into character data meaning that I am trying to input character data datastep written above?
Hope someone can help me. Thanks.
Kim
2 REPLIES 2
Cynthia_sas
SAS Super FREQ
Hi, Kim:
This does not really seem like an ODS question, but more of a DATA step INFILE/INPUT statement question.

As you noted, the macro variable &x is NOT resolved in the input buffer, so when your INPUT statement tries to read it from the buffer, SAS sees non-numeric characters where it is expecting to see numeric characters. In fact, the documentation on the SAS Macro facility states that:
"You can define and use macro variables anywhere in SAS programs, except within data lines." Since &x is a macro variable reference you cannot "technically" use it within your data lines.

If all you need to do is change con2 to the value of &x for the "beta" row (when _n_ = 1), then this would work in your existing program (as you have presented the hypothetical data):
[pre]
if firms = 'beta' and _n_ = 1 then do;
con2 = input("&x",8.0);
end;
[/pre]

If &x is in the global symbol table when the DATA step program executes, then the value for &x will be translated from character to numeric by the INPUT function (different from the INPUT statement). You will still see a warning in the LOG that there was a problem trying to read &x as a number.

I worry a bit about your usage of the term "script" and your reference to using CALL SYMPUT to set the value of X. Generally, you use CALL SYMPUT within a DATA step program to set the value of a macro variable (in the global symbol table) from some value within a DATA step program at execution time. This implies to me, somehow that the value of the macro variable &x might be changing based on the value of some variable in some -other- DATA step program.

A DATA step program has similarities to scripting languages (like JavaScript, PHP or VB), but I would not consider a DATA step program to be a script. Also, your DATA step program (where you use call symput), could itself be contained within a SAS macro program -- and if this is the case, then, the above solution might not be the best one for your ENTIRE program, including the piece with the CALL SYMPUT. If you are not familiar with SAS macro programming conventions, you might consider contacting Tech Support with the entire text of your program to make sure that you are using the Macro facility in the best way. It seems unusual (to me) that a single variable value in "raw" datalines or a "raw" data file would change based on a macro variable.

To contact Tech Support, go to:
http://support.sas.com/ctx/supportform/index.jsp

cynthia
deleted_user
Not applicable
When I had a problem like this under z/OS, I used the RESOLVE function:

INFILE ... LENGTH=RECLGTH;
INPUT @;
INPUT @1 RECORD $VARYING256. RECLGTH;
RECORD=RESOLVE(RECORD);

Of course, you then need to split up the record into the separate variables! Maybe the SCAN function could be used to do this.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 585 views
  • 0 likes
  • 3 in conversation