Irene
Irene

Reputation: 47

SAS Macros in Datalines

I have a two part question about creating datasets in SAS that calls upon macro variables

Part 1

I'm trying to create a dataset that has one character variable called variable with a length of 100, and 3 observations.

%let first_value=10;
%let second_value=20;
%let third_value=30;

data temp;
    infile cards truncover;
    input variable $100.;
    cards;
        First Value: &first_value
        Second Value: &second_value
        Third Value: &third_value
    ;
run;

My output dataset doesn't show the macro variables, just the exact text I entered in the datalines. I would love help on syntax of how to concatenate character input with a macro variable. Also I'm curious why sometimes you need a separate length statement for character variables before the input statement when other times you can just specify the length in the input statement like above.

Part 2

Next, I'm trying to create a dataset that has one observation with 4 variables, 3 of which are macro variables.

data temp2;
    infile cards dlm="    "
    input variable $ first_var second_var third_var
    cards;
        Observation 1 Filler    &first_value    &second_value    &third_value
    ;
run;

The 4 spaces in the delimiter statement and between variables in the datalines are actually tabs in my code.

Thanks!

Upvotes: 0

Views: 417

Answers (1)

Tom
Tom

Reputation: 51566

Your examples do not seem to be worth using macro variables.

But if you really need to resolve macro expressions in variable values then use the RESOLVE() function. The RESOLVE() will evaluate all macro code in the text, not just the macro variable references in your example. So any macro function calls and calls to actual macros will be resolved and the generated text returned as the result of the function.

newvar=resolve(oldvar);

So your examples become:

data temp;
  infile cards truncover;
  input variable $100.;
  variable = resolve(variable);
cards;
First Value: &first_value
Second Value: &second_value
Third Value: &third_value
;

data temp2;
  infile cards dlm="|" ;
  input @;
  _infile_=resolve(_infile_);
  input variable :$100. first_var second_var third_var ;
cards;
Observation 1 Filler|&first_value|&second_value|&third_value
;

But on the second one be careful as the _INFILE_ variable for CARDS images are fixed multiples of 80 bytes so if the resolved macro expressions make the string longer than the next 80 byte boundary you will lose the extra text.

511  %let xx=%sysfunc(repeat(----+----0,8));
512
513  data test;
514    infile cards truncover;
515    input @;
516    _infile_=resolve(_infile_);
517    input variable $100. ;
518    length=lengthn(variable);
519    put length= variable=;
520  cards;

length=5 variable=short
length=80 variable=long ----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+
NOTE: The data set WORK.TEST has 2 observations and 2 variables.

So use input from an actual file instead. That way the limit is instead the 32,767 byte limit for a character variable.

%let xx=%sysfunc(repeat(----+----0,8));
options parmcards=text;
filename text temp;
parmcards;
short
long &xx
;

531
532
533  data test;
534    infile text truncover;
535    input @;
536    _infile_=resolve(_infile_);
537    input variable $100. ;
538    length=lengthn(variable);
539    put length= variable=;
540  run;

NOTE: The infile TEXT is:
      Filename=C:\...\#LN00053,
      RECFM=V,LRECL=32767,File Size (bytes)=17,
      Last Modified=08Jul2022:23:42:10,
      Create Time=08Jul2022:23:42:10

length=5 variable=short
length=95 variable=long ----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+----0----+----0
NOTE: 2 records were read from the infile TEXT.
      The minimum record length was 5.
      The maximum record length was 8.
NOTE: The data set WORK.TEST has 2 observations and 2 variables.

Upvotes: 3

Related Questions