## SYMGET

Solved
Occasional Contributor
Posts: 7

# SYMGET

Hi ,

I am calculating Processing , start time and count of nid and wanted to get them into dataset sas

%let Rcc = 66;

%let ty=p;

%let datetime_start = %sysfunc(TIME());

%let START_TIME_WITHV&&Rcc = %sysfunc(datetime(),datetime14.);

%put &&START_TIME_WITHV&Rcc;

data scores;

infile datalines truncover;

input nid \$ 1-12 score2 17-20 score1 27-30;

datalines;

Riley 1132 987

Henderson 1015 1102

;

%let END_TIME_WITH_V&&Rcc= %sysfunc(datetime(),datetime14.);

%put &&END_TIME_WITH_V&Rcc;

%let PROCESSING_TIMEV&&Rcc= %sysevalf(%sysfunc(TIME())-&datetime_start.);

%put &&PROCESSING_TIMEV&Rcc;

proc contents data= scores out=ty_contents;

run;

proc sql noprint;

select name into :score_t_&&Rcc

from ty_contents

where prxmatch("s/nid//i",name);

select count(distinct &&score_t_&Rcc) into :s_cnt&&Rcc from scores(keep=&&score_t_&Rcc);

quit;

%put &&score_t_&Rcc &&s_cnt&Rcc;

/*count , start time and processing time variables into dataset */

data test5;

Date = SYMGET('START_TIME_WITHV&&Rcc');

length count&&Rcc_pp 3;

length V&&Rcc_Runtime 8;

count&&Rcc_pp = SYMGET('s_cnt&&Rcc');

V&&Rcc_time=SYMGET('PROCESSING_TIMEV&Rcc');

format V&&Rcc_time TIME11.2;

run;

How to get Date into test5 and also , how to get count into dataset , how to get processing time into test5 dataset

Thanks

Accepted Solutions
Solution
‎12-10-2017 11:40 PM
Super User
Posts: 8,279

## Re: SYMGET

[ Edited ]

The SAS LOG should show a pretty clear error message.

```11   %let Rcc = 66;
12   %let START_TIME_WITHV&&Rcc = %sysfunc(datetime(),datetime14.);
13   %put &&START_TIME_WITHV&Rcc;
09DEC17:20:39
14   data _null_;
15     x1=symget('START_TIME_WITHV&Rcc');
16     put x1=;
17   run;

INFO: Character variables have defaulted to a length of 200 at the places given by:
(Line):(Column). Truncation can result.
15:3     x1
ERROR: Symbolic variable name START_TIME_WITHV&RCC must contain only letters, digits, and
underscores.
NOTE: Invalid argument to function SYMGET('START_TIME_W'[12 of 20 characters shown]) at line 15
column 6.
x1=
x1=  _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.```

The SYMGET() function wants the NAME of the macro variable, but macro variable names cannot contain &.  If you want the &RCC suffix to be evaluated so that SYMGET() can use the actual macro variable's name then you must use double quotes instead of single quotes.

``symget("START_TIME_WITHV&Rcc")``

But then again why use SYMGET()?  There is nothing in the data step that is changing the value of the macro variable.

``Date = "&&START_TIME_WITHV&&Rcc";``

If you want DATE to be a number and not a character string then perhaps you want to use a datetime literal?

``````Date = "&&START_TIME_WITHV&&Rcc"dt;
format date datetime14.;``````

Of course if want the real value then do not generate the value using the DATETIME14 format since that will lose precision.

Instead leave it as the raw number of seconds.

``````%let START_TIME_WITHV&&Rcc = %sysfunc(datetime());
.....
Date = &&START_TIME_WITHV&&Rcc;
format date datetime20.;``````

All Replies
SAS Super FREQ
Posts: 508

## Re: SYMGET

Macro variables are not resolved within single quotes.  Use double quotes if you want macro variables to resolve.  I did not try to understand your program, but I noticed that much.

Occasional Contributor
Posts: 7

## Re: SYMGET

[ Edited ]

I want to get these values  'START_TIME_WITHV&&Rcc' , 's_cnt&&Rcc' , 'PROCESSING_TIMEV&Rcc'  into datastep

and create  these variables Date ,  count&&Rcc_pp and V&&Rcc_time respectively in a dataset test5

Occasional Contributor
Posts: 7

## Re: SYMGET

Thank you that worked

Solution
‎12-10-2017 11:40 PM
Super User
Posts: 8,279

## Re: SYMGET

[ Edited ]

The SAS LOG should show a pretty clear error message.

```11   %let Rcc = 66;
12   %let START_TIME_WITHV&&Rcc = %sysfunc(datetime(),datetime14.);
13   %put &&START_TIME_WITHV&Rcc;
09DEC17:20:39
14   data _null_;
15     x1=symget('START_TIME_WITHV&Rcc');
16     put x1=;
17   run;

INFO: Character variables have defaulted to a length of 200 at the places given by:
(Line):(Column). Truncation can result.
15:3     x1
ERROR: Symbolic variable name START_TIME_WITHV&RCC must contain only letters, digits, and
underscores.
NOTE: Invalid argument to function SYMGET('START_TIME_W'[12 of 20 characters shown]) at line 15
column 6.
x1=
x1=  _ERROR_=1 _N_=1
NOTE: The SAS System stopped processing this step because of errors.```

The SYMGET() function wants the NAME of the macro variable, but macro variable names cannot contain &.  If you want the &RCC suffix to be evaluated so that SYMGET() can use the actual macro variable's name then you must use double quotes instead of single quotes.

``symget("START_TIME_WITHV&Rcc")``

But then again why use SYMGET()?  There is nothing in the data step that is changing the value of the macro variable.

``Date = "&&START_TIME_WITHV&&Rcc";``

If you want DATE to be a number and not a character string then perhaps you want to use a datetime literal?

``````Date = "&&START_TIME_WITHV&&Rcc"dt;
format date datetime14.;``````

Of course if want the real value then do not generate the value using the DATETIME14 format since that will lose precision.

Instead leave it as the raw number of seconds.

``````%let START_TIME_WITHV&&Rcc = %sysfunc(datetime());
.....
Date = &&START_TIME_WITHV&&Rcc;
format date datetime20.;``````
Occasional Contributor
Posts: 7

## Re: SYMGET

Thank you that worked :-)

Occasional Contributor
Posts: 7

## Re: SYMGET

Hi ,
Above one solves for date how to create variables for other two

count&&Rcc_pp = Symget(‘s_cnt&&Rcc’);
V&&Rcc_time = symget(‘Processing_timev&&Rcc’);
Super User
Posts: 10,578

## Re: SYMGET

Several issues here.

Since you omitted a

``run;``

to end your data step, the %let's that should determine the execution time are resolved before the data step runs, so you'll get no time at all.

The data step itself will execute so quickly that you will have to look for fractions of a second.

In the final data step, your symget can't work because of the single quotes that prevent the resolution of &rcc.

And if you want to reference a macro variable inside a word, you MUST use a dot to end the reference, like

``count&rcc._pp``

, or SAS will look for a macro variable rcc_pp, which was never defined. Bottom line: always end a macro variable reference with a dot, it's never wrong and always helpful.

And be more consistent with your use of single and double ampersands, to make code more readable. Double ampersands are only needed when you refer indirectly and need two passes of the macro resolver.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.