BookmarkSubscribeRSS Feed
nilu10686
Calcite | Level 5

Hi All,

In an interview  of SAS, I have asked this question that In my dataset if I have 1000 variables so I want to create new variable @ 5th place or 50th place what method I should choose?

Any help appreciated.

4 REPLIES 4
SASKiwi
PROC Star

This is one way:

data want;
  retain Var1 Var2 Var3 Var4 NewVar;
  set have;
  NewVar = "NewVal";
run;

But IMHO it's a dumb question as the position of a variable has no effect on processing or analysis. The only time I reposition variables is to regroup them for browsing in a viewer. 

PaigeMiller
Diamond | Level 26

@SASKiwi wrote:

But IMHO it's a dumb question as the position of a variable has no effect on processing or analysis. The only time I reposition variables is to regroup them for browsing in a viewer. 


I give extra points for this answer, if I'm asking the question in an interview.  

--
Paige Miller
ChrisNZ
Tourmaline | Level 20

For 50th place, I'd use

data WANT;
  if 0 then set HAVE (keep=VAR1--VAR49);
  length NEWVAR 8;
  if 0 then set HAVE (keep=VAR50--VAR99);
  .. normal SAS code ...
run;

but as @SASKiwi  said that's not an interesting question.

 

Ksharp
Super User
Same as ChrisNZ:

data have;
retain var1-var1000 1;
run;


data want;
if 0 then set have(keep=var1-var50);
length new_var $ 200 ;
set have;
new_var='NewVariable';
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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