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

Hi all ,

I have a data set having values as described below:

A

B

C

AB

BC

CA

ABC

104

112

158

40

46

6

58

 

I want to convert all the columns in to individual sas macro variables based on the current column names they have.

 

Rather than writing a proc SQL statement, is there any other shorter way to do achieve that? Kindly share your thoughts.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input A	B	C	AB	BC	CA	ABC;
cards;
104	112	158	40	46	6	58
;
data _null_;
 set have;
 array x{*} _all_;
do i=1 to dim(x);
 call symputx(vname(x{i}),x{i});
end;
run;

%put _user_ ;

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

@sahoositaram555 wrote:

Hi all ,

I have a data set having values as described below:

A

B

C

AB

BC

CA

ABC

104

112

158

40

46

6

58

 

I want to convert all the columns in to individual sas macros based on the current column names they have.

 

Rather than writing a proc SQL statement, is there any other shorter way to do achieve that? Kindly share your hought


It's hard to imagine something quicker than SQL, if you must do this (although since you ask for a shorter way, it may be that you don't need macro variables at all here, as in general you don't want data to be stored in macro variables; there's nothing easier than NOT turning this into macro variables; but you'd have to explain more for us to know).

 

Terminology note so we all use the same language: you are talking about macro variables and not macros.

--
Paige Miller
sahoositaram555
Pyrite | Level 9

Thanks @PaigeMiller,

 

It was a mistake from my end. I have modified my query as suggested.  

Reeza
Super User
You can use a data step with an array. VNAME() will get the variable name. It's more dynamic than SQL because you don't need to list all the names.

Or transpose it to a long format and then use the variable names.
novinosrin
Tourmaline | Level 20

@Reeza  Very smart thinking coz proc transpose method is a safe bet that handles combination of char and num/both or none as opposed to array where tyhe WHAT-IF check can complicate if there is only one that exist in some dataset and both exist in some other. Kudos! Now I believe you are moving to coffee from tea lol

novinosrin
Tourmaline | Level 20

Going with @Reeza  neat suggestion of proc transpose 

 


data have;
input A	B	C	AB	BC	CA	ABC;
cards;
104	112	158	40	46	6	58
;

proc transpose data=have(obs=0) out=temp;
var _all_;
run;
data _null_;
set temp;
call symputx(_name_,_name_,'g');
run;

%put _global_;
FreelanceReinh
Jade | Level 19

@novinosrin: I think what @Reeza had in mind was not using "(obs=0)" in PROC TRANSPOSE and col1 rather than _name_ as the second argument of CALL SYMPUTX.

Ksharp
Super User
data have;
input A	B	C	AB	BC	CA	ABC;
cards;
104	112	158	40	46	6	58
;
data _null_;
 set have;
 array x{*} _all_;
do i=1 to dim(x);
 call symputx(vname(x{i}),x{i});
end;
run;

%put _user_ ;

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
  • 8 replies
  • 1533 views
  • 3 likes
  • 6 in conversation