BookmarkSubscribeRSS Feed
DaveStar
Obsidian | Level 7

So, as I can see the best solution is :

data w;
a
=&m0;
b
=&m1;
c
=&m2;
d
=&m3;
e
=&m4;

f=&m5;
run;

and then to use proc transpose to define a data set with one column.

True?

PaigeMiller
Diamond | Level 26

@DaveStar wrote:

So, as I can see the best solution is :

data w;
a
=&m0;
b
=&m1;
c
=&m2;
d
=&m3;
e
=&m4;

f=&m5;
run;

and then to use proc transpose to define a data set with one column.

True?


There is no need to use macro variables here. There is no need to PROC TRANSPOSE.


Why are you insisting on using macro variables in a data step this way?

 

Just enter the data into the data step (after the CARDS statement), with no macro variables.

--
Paige Miller
DaveStar
Obsidian | Level 7

I think that you don't understand the issue.

I know how to enter values directly into data step.

In this case the macro parameters are values that I get from "external code" of my friends at work.

So....I am using these values to create a data set.

Instead of writing it manually every day (Which can cause errors due to typing errors) I want to know how to create a data set from these values 

 

Dave

PaigeMiller
Diamond | Level 26

Use a SAS data set to transfer data. Or, use a text or CSV file to transfer data.

 

 

--
Paige Miller
VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

this looks like the macro variable I setup at work about 19 years ago because of the length of 4 and back then variables could only be 8 char in length.  Smiles %)  I would strongly suggest @DaveStar to talking to your friends at work on how to handle your teams requirements....

 

DaveStar
Obsidian | Level 7

Thank you anyway

DaveStar
Obsidian | Level 7

Your answer is similar to following question:

Question:What is the best diet?

answer: Not eating

 

ScottBass
Rhodochrosite | Level 12

@DaveStar wrote:

Your answer is similar to following question:

Question:What is the best diet?

answer: Not eating

 


 

Edit:  What @hashman said - I hadn't seen his answer yet before posting.

 

A bit of advice:

 

1) The approach in your original post sucks.

2) It sucks whether you want to believe it or not.

3) Smarter more experienced SAS programmers on this board are trying to help you.

4) Your flippant comment above doesn't help us want to help you.

 

If your poorly designed data transfer processing really IS macro variables, then here is an approach:

 

%let m0=1906;
%let m1=1905;
%let m2=1904;
%let m3=1903;
%let m4=1902;
%let m5=1901;
%let m6=1812;

data stuff;
   set sashelp.vmacro;
   where prxmatch('/M\d+/',name) and scope='GLOBAL';  * danger of grabbing macro variables you don't want ;
   date=input(cats(value,'01'),yymmdd6.);  * uses YEARCUTOFF value ;
   * or date=input(cats('20',value,'01'),yymmdd8.);  * if all years are in this century ;
   * keep date;
   format date yymmddn8.;
run;

 

Then deal with your data as it was meant to be transferred to you in the first place.


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.
Astounding
PROC Star

While the posts that suggest the whole process could be improved might be right, let me address the question that you started out asking.  Here's how a DATA step can turn your macro variables into DATA step values for DATENAME:

 

data want;
   DateName = input("20&m0.01", yymmdd8.);
   output;
   DateName = input("20&m1.01", yymmdd8.);
   output;
   DateName = input("20&m2.01", yymmdd8.);
   output;
   DateName = input("20&m3.01", yymmdd8.);
   output;
   DateName = input("20&m4.01", yymmdd8.);
   output;
   DateName = input("20&m5.01", yymmdd8.);
   output;
   DateName = input("20&m6.01", yymmdd8.);
   output;
   format DateName date9.;
run;

It's not clear that the final process you choose will need to use DATENAME.  However, that's the first unanswered question so I thought I would start there.  In my opinion, it's important for you to understand the steps of the process so I didn't try to skip over any intermediate steps.

DaveStar
Obsidian | Level 7

Thank you so much!

It is the solution and I have learned a lot

 

hashman
Ammonite | Level 13

@Ronein:

If just want to hard code the macro variables' names in a DATA step, there're many ways to get what you want without using the CARDS/LINES/DATALINES, for example:

%let m0 = 1906 ;                                                                                                                        
%let m1 = 1905 ;                                                                                                                        
%let m2 = 1904 ;                                                                                                                        
%let m3 = 1903 ;                                                                                                                        
%let m4 = 1902 ;                                                                                                                        
%let m5 = 1901 ;                                                                                                                        
%let m6 = 1812 ;                                                                                                                        
                                                                                                                                        
data want ;                                                                                                                             
  do YYMM = &m0, &m1, &m2, &m3, &m4, &m5, &m6 ;                                                                                          
    output ;                                                                                                                            
  end ;                                                                                                                                 
run ;            

Another way of going around CARDS is using SQL's INSERT clause instead where the macro variables will resolve just fine.

 

Frankly, I can't see how hard coding M0, M1, ... Mn can be practically utile. Even if you had a requirement of this sort, it would be normally given in a more general form, e.g.: Create a data set with a numeric YYMM variable whose values correspond to those of existing macro variables whose names start with M with digits only after that. In this case, you can construct a SYMGET function agrument based on this info, or auto-generate the DO loop above, or do many other things.  

 

However, since you want to create a data set, why bother with any of the above if the names of all macro variables along with their values are already auto-stored in the system table dictionary.macros that can be read by SQL or accessed in the DATA step via the view sashelp.vmacro? Simply read either and filter  by the macro scope and name as need be; for example:

data want (keep = YYMM) ;                                                                                                               
  set sashelp.vmacro (keep = name value) ;                                                                                              
  where scope =: "G" and name =: "M" and notdigit (substr (trim (name), 2)) = 0 ;                                                                        
  YYMM = input (value, ?? yymmn4.) ;
format yymm date9. ; run ;

Kind regards

Paul D.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 25 replies
  • 2040 views
  • 11 likes
  • 11 in conversation