BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
helpmedoubts
Fluorite | Level 6

data one;
input variable$1-8;
datalines;
var1 $10
var2 $9
var3 $11
;
run;
Now i need to create a macro &x.using the above dataset
data two;
input &x;
datalines;
a b c
;
run;
macro &x should resolve like this - var1 $10 var2 $9 var3 $11
I couldn't get the slightest idea how to do this. Can someone help with this?

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Your original post stated that you want to create an INPUT statement using the macro variable &X.  As others have mentioned, that gives you a program that does the wrong thing.

 

However, you later clarified that saying that you want the macro variable to be used in a LENGTH statement.  That is quite possible.  After creating the data set ONE, you would use:

proc sql;
select variable into : x separated by ' ' from one;
quit;

%put &X;

Now you have the macro variable X with the desired value, and you can experiment with using it any way that you see fit.

View solution in original post

12 REPLIES 12
PaigeMiller
Diamond | Level 26

First of all, this really isn't clear. You mention you want a "macro", but it seems like you really want a "macro variable". "Macro" and "macro variable" are not the same thing. Please confirm you really want a "macro variable".

 

Next, even if you get a macro variable named &X that has the value you want, it will not produce a meaningful result in DATA TWO if you run it. You need to make sure that the macro variable, when replaced by its actual value, produces legal valid working SAS code (and this isn't working properly).


If I am understanding you properly, in DATA TWO, you want the second line to say (after replacing &X with its value)

 

input var1 $10 var2 $9 var3 $11;

but this will not work. It will read the data incorrectly. These are the things that you have to think about and fix before you try to create a macro variable and use it. What should this INPUT statement say? If you had to write it without macro variables, show me an INPUT statement that will actually work here and produce the desired result. To me, creating code that works properly without macro variables is a MANDATORY (not optional) first step.

 

What is the desired result from DATA TWO anyway? You don't tell us.

--
Paige Miller
helpmedoubts
Fluorite | Level 6

Sorry, I need to create a macro variable &x. and its should resolve like this

input var1 $10 var2 $9 var3 $11

I just gave some random values for those variables. All I need is to create a macro variable and it should resolve like mentioned above. Sorry for the confusion.

 

PaigeMiller
Diamond | Level 26

But as I said, this INPUT statement will read the data incorrectly.

 

What should the INPUT statement say (without macro variables, using only SAS data step code) so that it will read the data correctly?

 

I really want you to understand the thought process here. Your macro variable, when it is replaced by its value, must produce working SAS code. You need to have that working SAS code before you can create a macro variable. You can't just create any text string in your macro variable &X and expect it to do what you want.

 

If you don't have working SAS code to start with in DATA TWO, then using a macro variable will fail as well.

--
Paige Miller
helpmedoubts
Fluorite | Level 6

Sorry, Now I understood your point.

I need dataset two like this based on dataset one

data one;
input variable$1-8;
datalines;
var1 $10
var2 $9
var3 $11
;
run;

crreate a macro variable &x.

length &x.; should resolve length var1 $10 var2 $9 var3 $11;

data two;
length var1 $10 var2 $9 var3 $11;
input var1 $ var2 $ var3 $;
datalines;
a b c
;
run;

Hope I am clear this time.

Shmuel
Garnet | Level 18

Data Two was defined as (taken from the original post):

data two;
input &x;
datalines;
a b c
;
run;

Then suppose the macro variable is assigned by %LET, as in:

%let x = var1 $10 var2 $9 var3 $11 ;
data two;
input &x;
datalines;
a b c
;
run;

then it will resolve to:

data two;
input var1 $10 var2 $9 var3 $11;
datalines;
a b c
;
run;

and it will run without errors.

 

If I understand correctly, the target is to create the macro variable X in data One,

then the code to do it is:

data one;
  infile datalines eov=eof;
  length x $30; retain x;
  input variables $1-8;
  x = catx(' ',x,variables); 
  call symputx('x',x);
datalines4;
var1 $10
var2 $9
var3 $11
;;;;
run;
%put x= &x;

 

Kurt_Bremser
Super User

@helpmedoubts wrote:

data one;
input variable$1-8;
datalines;
var1 $10
var2 $9
var3 $11
;
run;
Now i need to create a macro &x.using the above dataset
data two;
input &x;
datalines;
a b c
;
run;
macro &x should resolve like this - var1 $10 var2 $9 var3 $11
I couldn't get the slightest idea how to do this. Can someone help with this?


This is not possible; the data in dataset one can't be used to create working code. You need to fix dataset one first, before you can use it to create the macro variable.

 

Astounding
PROC Star

Your original post stated that you want to create an INPUT statement using the macro variable &X.  As others have mentioned, that gives you a program that does the wrong thing.

 

However, you later clarified that saying that you want the macro variable to be used in a LENGTH statement.  That is quite possible.  After creating the data set ONE, you would use:

proc sql;
select variable into : x separated by ' ' from one;
quit;

%put &X;

Now you have the macro variable X with the desired value, and you can experiment with using it any way that you see fit.

helpmedoubts
Fluorite | Level 6

Thank you so much. It worked.

Tom
Super User Tom
Super User

If you want to use metadata (ONE) to generate code then make sure the metadata has the information you need.

Your example seems to have mashed two variables into into one. The name of the variable and the length of the variable.  I say length instead of format because the values you list do not have the period needed to be format specifications, but are valid syntax for setting the length of a variable in a LENGTH statement.

 

You also need to know what code you want to generate.  Your proposed code does not look right.  Do you really want VAR1 to be the single character in column 10 of the input line? And VAR2 to the single character in column 9?

 

So assuming your values are lengths you could modify your code like this to do something reasonable.  (It is still not clear if it is what you want to do.)

data one;
  varnum+1;
  input variable :$32.  length :$6.;
datalines;
var1 $10
var2 $9
var3 $11
;

proc sql noprint;
  select catx(' ',variable,length) 
     into :lengths separated by ' '
     from one
     order by varnum
  ;
quit;

data two;
   length &lengths;
   input (_all_) (+0);
datalines;
a b c
;

 

helpmedoubts
Fluorite | Level 6

Thank you. This code worked. The values I gave in the original dataset were single characters. I just gave them as an example. My main doubt was how to create a macro variable. Thanks a lot for the code. it worked perfectly. But I couldn't understand this line

input (_all_) (+0);

 Could you please explain this?

I never saw _all_ in input statement. It's very interesting. Learning something new everyday from this community.

Tom
Super User Tom
Super User

_ALL_ is a variable list.  Just like A B C or VAR1-VAR2 or _NUMERIC_ or _CHARACTER_.

The parentheses are used when specifying a list of variables followed by a list of informats in a INPUT (or formats in a PUT) statement.

The +0 is a cursor movement command that will move the  cursor by zero character positions.  You just need it so the list of informats is not empty.

helpmedoubts
Fluorite | Level 6

Thank You!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 12 replies
  • 1404 views
  • 2 likes
  • 6 in conversation