BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

Hi can you explain to me what call symput does, I read the manual and cannot understand..

 

also could you also explain put and input with a simple example, I also struggle with what is said in the manual

 

thanks a lot...

6 REPLIES 6
japelin
Rhodochrosite | Level 12

call symput can store the value of a variable into a macro variable in the data step
In the following, the value of Alfred's SEX is stored in the macro variable "MACRO_SEX".

 

data sample0;
  set sashelp.class;
  if(name='Alfred')then do;
    call symput('MACRO_SEX',SEX);
  end;
run;

%put &=MACRO_SEX;

 

 

Please note that there are statements and functions for put and input.
The put function converts a numeric variable to a character variable, and the input function converts a character variable to a numeric variable
The put statement outputs the value of a variable to a log or file, and the input statement imports the value specified in a file or datalines.

 

Convert age to character with put function

data sample2;
  set sample1;
  AGE_N=input(AGE_C,3.);
run;

Converting characters to numbers with the input function

 

data sample1;
  set sashelp.class;
  AGE_C=put(age,3.);
run;

 

 

Output name and age to the log with put statement

data sample3;
  set sashelp.class;
  put name age;
run;


Converting Values Described in Datalines into Data Sets with the input Statement

data sample;
  input name $ age;
datalines;
ALFRED 14
ALICE 13
;
run;

 

Kurt_Bremser
Super User

@japelin wrote:


The put function converts a numeric variable to a character variable, and the input function converts a character variable to a numeric variable


The second part of this is not (completely) true; while the INPUT function is typically used for this type of conversion, it can yield a character result also:

proc format;
invalue $insex (default=1)
  "male" = "M"
  "female" = "F"
;
run;

data want;
input instring $6.;
sex = input(instring,$insex.);
datalines;
male
female
;

In the resulting dataset, sex will be a character variable of length 1.

 

HeatherNewton
Quartz | Level 8

I was not sure if it has to be a macro variable that the value of the variable is assigned to as looking at it, it does not like relating to macro variables...

Tom
Super User Tom
Super User

It helps to know that SAS will also use the term SYMBOL for macro variables.    Hence the SYM in CALL SYMPUT.

 

You should also know that you should limit the use of CALL SYMPUT() to situations where you need to store leading and/or trailing spaces into the symbol or macro variable.  99% of the time you want to use the newer and more powerful CALL SYMPUTX() function instead.

 

To understand PUT, PUT(), PUTN(), PUTC(),  INPUT, INPUT(), INPUTN() and INPUTC() you need to understand FORMATs and INFORMATs.

 

A format is instructions for how to convert values into TEXT.  An informat is instructions for how to convert TEXT into values.

 

You use the INPUT statement to read from text into values.  If you use a numeric informat then the resulting value is a number.  If you use a character informat then the value is a character string.  The INPUT() function allows you to use the informat it situations where the source of the text is a variable or other SAS expression and the target is a variable (or part of larger SAS expression).  What type of value the INPUT() produces depends on the type of informat used.  With the INPUTN() or INPUTC() the type of value is explicit in the name of the function.  With INPUTN() and INPUTC() the informat specification to use is taken from a character expression.  So you can generate what informat to use dynamically.  Or you could use a constant value, but instead of code syntax you have to pass it as a string literal.

 

You use the PUT statement to write value as text to a file (which includes the SAS log file or output window).  If the value you are writing is a number you need to use a numeric format.  If the value is character you need to use a character format.  The  PUT() function allows you to the format in situations where the source of the vlaue is a variable or other expression and the target is a variable (or part of larger SAS expression).

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 778 views
  • 5 likes
  • 4 in conversation