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

Hi all,

 

I am a SAS-novice, and working through the book of Andy Field and Jeremy Miles "Discovering statistics using SAS".

I am encountering all kind of smaller hiccups doing the exercises in the book, what has to do with the SAS-program (and not with the statistics itself).

 

What is the proper platform/community for my questions. I will give one example.

 

One of the datafiles to be used for the exercises misses for some unknown reason a vairiable ID. I now want to create a variable (vector) ID with 50 records from 1 to 50, and to add that to the datafile. Of course I can add the data manually, but I want to learn to write some simple code. In R this is no problem for me. But is SAS I ran into troubles.

 

I created a vector V as follows:

 

PROC IML;

V=T(1:50);

 

Then aI tried to add that to the dataset

 

DATA set2; SET set1;

ID=V;

RUN;

 

Then in set2 two new empty variables have been created ID and V.

 

Thanks for the help.

 

Wil Tamis

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

_n_ is an automatic variable, that iterates at the end of each data step iteration. 

 

It's not a row counter, but can be used as a row counter unless you're doing some complex data steps. 

 

It sounds like you have Base SAS, not EG (point and click interface) or SAS Studio (accessed via web browswer).

 

https://communities.sas.com/t5/forums/replypage/board-id/community_discussion/message-id/1323

 

If you're new to SAS, the SAS Analytics U page has training resources, free and paid, on the link on the right hand side. The first SAS programming e-course is free. 

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

Most of your questions can be posted under SAS Programming >Base SAS Programming.

 

 

Proc IML is not part of Base SAS. You can mix and go between data steps and Proc IML, but if you're new it might not be the best idea.

 

IML treats data like R, arrays/matrices and column functions. Base SAS does not, it goes through data line by line. 

 

That being said, if you want to add a row identifier of 1 to 50 to your data set you can use the automatic variable _n_. I'm not sure what you're trying to do with the 1 to 50 so this may or may not make sense. A great way to ask questions is to show the current structure/sample of your data and your desired output. 

 

DATA set2; 
SET set1;
ID=_n_;
RUN;
WilTamis
Calcite | Level 5

Thanks for the answer (it works) and the tips.

 

So, I have SAS 9.3, apparently dat is Base SAS?

 

Still have to find out why your solution works (_n_).

Reeza
Super User

_n_ is an automatic variable, that iterates at the end of each data step iteration. 

 

It's not a row counter, but can be used as a row counter unless you're doing some complex data steps. 

 

It sounds like you have Base SAS, not EG (point and click interface) or SAS Studio (accessed via web browswer).

 

https://communities.sas.com/t5/forums/replypage/board-id/community_discussion/message-id/1323

 

If you're new to SAS, the SAS Analytics U page has training resources, free and paid, on the link on the right hand side. The first SAS programming e-course is free. 

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

Discussion stats
  • 3 replies
  • 1150 views
  • 2 likes
  • 2 in conversation