DATA Step, Macro, Functions and more

Validate column names and fetch data into macro variables

Reply
Contributor
Posts: 65

Validate column names and fetch data into macro variables

[ Edited ]

Hi,

 

I have imported data from excel to a sas-data set. The dataset after exporting looks like this.

 

Name| ID | Salary | Designation

------------------------------------------------

ABC  | 11 | 2000$ | Manager

 

The table will only have 1 column .

Now I want to validate the column label if an if condition and store the value of column in a macro variable:-

 

pseudo code:-

if macro_label="Name"
then do;
call symput(symget('myMVar'),Name);
end;

Please suggest a way to validate the column labels of a sas dataset.

 

Super User
Posts: 6,928

Re: Validate column names and fetch data into macro variables

You cannot export data from Excel to a SAS dataset; Excel cannot write SAS datasets.

So please describe exactly what you have done to get the data into SAS (which file format was written from Excel, and which step was used in SAS for import)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Super User
Posts: 7,392

Re: Validate column names and fetch data into macro variables

If you have have imported the Excel file into SAS then the metadata will be present in the metadata tables.  For instance, if I have a dataset ABC in WORK, then:

data _null_;
  set sashelp.vcolumns (where=(libname="WORK" and memname="ABC" and name="COL1"));
  call symputx('myVar',name);
run;

Would work.  However, why put a column label in a macro variable.  It is already present in the metadata tables, so when you want to use it, pull the information from there.  Otherwise you may hit several road blocks (invalida characters, line lengths for multiples, multiple values etc.).  If you can elaborate on what you are doing we can look.

 

 

Trusted Advisor
Posts: 1,607

Re: Validate column names and fetch data into macro variables

Hello, @Aditi24, I'm really having trouble understanding your problem. Could you give specific examples of what macro variables you want to create from this input data, and what values they should have?

 

Hello, @RW9, you ask "However, why put a column label in a macro variable." Because that's one legitimate use of macro variables, to contain and make use of text that might vary from one execution of the program to another.

 

 

Super User
Super User
Posts: 7,392

Re: Validate column names and fetch data into macro variables

Please do stop spamming my posts where I ask simple questions.  The reason I asked the question is clearly stated in the text:

"However, why put a column label in a macro variable.  It is already present in the metadata tables, so when you want to use it, pull the information from there."

Of which you have only quoted part.  In most scenarios it would be quicker and simpler to refer to an existing dataset which contains the required information than to create a whole set of coding to emulate that which is already freely available.  If there are specific set of circumstances where this is needed, then other points have to be raised about sepcial characters when de-referencing etc.

Trusted Advisor
Posts: 1,607

Re: Validate column names and fetch data into macro variables

Ok, RW9, I'll quote the whole thing next time. I still state that putting text into a macro variable for use in a program is a perfectly legitimate thing to do.

Ask a Question
Discussion stats
  • 5 replies
  • 148 views
  • 0 likes
  • 4 in conversation